WPF LINQ DataContext刷新不读取sql数据库表中的新记录
我有一个 LINQ 数据上下文和一个 ObservableCollection,在 SQL 中保存 Companys 表的记录。
当我执行以下操作时,会反映对当前记录的更改以及对当前数据上下文 (DataDC) 所做的新记录。
DataDC.SubmitChanges();
但是,如果我有另一个带有单独 DataContext 的窗口相同的表,如果我修改当前存在于两个 DataContext 中的记录字段(即修改的公司名称),则当我执行以下操作时会看到这些更改:
DataDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Companys);
但是,如果其他窗口/数据上下文创建了新记录,或者如果我使用 SQL 资源管理器创建新记录记录,即使
DataDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Companys);
我尝试了不同的模式,这些新记录也不会显示在 DataContext.Company 表中。
那么为什么 .Refresh(....) 不加载新记录,但会反映对现有记录所做的更改?
我错过了什么吗?
我找不到一种方法来使用 SQL 表中的全新数据刷新 DC?
I have a LINQ datacontext and a ObservableCollection holding records for a Companys table in SQL
Changes to the current records and new records made to the current datacontext (DataDC) are reflected when I do a
DataDC.SubmitChanges();
However if I have another Window with a separate DataContext to the same tables, if I modify fields for records that currently exist in both DataContexts (ie Company Name modified) these changes are seen when I do a
DataDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Companys);
However if the OTHER window/datacontext created a new record or if I use SQL explorer to create a new record, these new records do not show in the DataContext.Company table even after a
DataDC.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, Companys);
I've tried different Modes.
So why doesn't a .Refresh(....) Load new records, but will reflect changes made to records that exist?
Am I missing something?
I can't see a way to just refresh the DC with completely all new data from the SQL tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Refresh()
方法不会从数据库中检索新记录。我不完全确定这是为什么 - 只是 Linq to SQL 产品团队做出的选择之一(大概有充分的理由)。这可能与刷新方法有关
要获取新输入的记录,您需要执行以下任一操作:
DataContext
上重新发出新查询DataContext
的新实例code>您选择哪个选项取决于上下文(没有双关语)。需要记住的事情是 Linq to SQL
DataContext
的预期工作单元性质以及Datacontext
预期的较短生命周期。The
Refresh()
method doesn't retrieve new records from the database.I'm not entirely sure why that is - just one of those choices that the Linq to SQL product team made (presumably with good reasons). It could have something to do with the fact that the refresh method
To get the newly entered records you will need to either:
DataContext
DataContext
Which option you go with depends on context (no pun intended). Things to bear in mind are the intended Unit of Work nature of the Linq to SQL
DataContext
and the intended short life span of theDatacontext
.如果您在同一个应用程序中拥有两个窗口,我认为没有任何理由不只使用一个 DataContext 并将原始数据上下文的引用传递给第二个窗口。
If you're having both windows in the same app, I don't see any reason not to use just one DataContext and pass a reference of your original datacontext to your second window.
抱歉,我发现了更多。
底层数据上下文确实反映了数据库中的变化。
这是我的责任,我假设
刷新了“集合”(公司),我的意思是这就是智能感知实际上所说的。但事实并非如此。
但是,如果您清除集合然后按如下方式重新加载它们,则这是有效的。问题不在于数据收集,只是 DataDC.Refresh 没有执行我认为的操作。
Appologies, Ive found out more.
The under lying Data Context DOES reflect the chnages live in the data base.
Its my fualt, I ASSUMED that
Refreashed the 'collection' (Companys) , I mean thats what intellisense actually says. But it doesnt.
But if you clear the collection and then reload them as below, this works. The problem was not with the data collection, just that DataDC.Refresh doesnt do what I thought it did.