LINQ 刷新模式如何工作?
如果发生冲突,我需要用我的更改覆盖数据库中的值。我在 MSDN 上找到了以下文章,解释了如何使用 RefreshMode 解决冲突:
http://msdn.microsoft.com/en-us/library/system.data.linq.refreshmode.aspx
我认为 KeepCurrentValues 对我的要求有意义,我还找到了此页面的示例对于此模式:
http://msdn.microsoft.com/en-us/library /bb399421.aspx
但是,当我实现此功能时,数据库中的值总是覆盖我的更改。我尝试将 RefreshMode 更改为 OverwriteCurrentValues 和 KeepChanges,并且每次保存数据库中的值。我的方法是在 VisualStudio 的调试模式下手动更改数据库中的值。 VS中的代码是:
[MyDataContext] db = new [MyDataContext]();
try
{
[MyLINQType] old = (from o in db.[MyLINQType] where o.ID=1 select o).Single();
old.IntField = 55;
// This is where I change the IntField value manually in the database in
// debug mode to generate the conflict.
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
}
我知道会出现冲突,但每次,无论我如何更改RefreshMode,值55都不会保存,并且会保留我在数据库中手动进行的更改。有什么技巧可以达到预期的效果吗?我一开始尝试从代码内部生成冲突,但这也没有按预期工作。也许我不明白 RefreshMode 应该如何工作。欢迎任何想法。
In case of conflict, I need to overwrite the values in the database with my changes . I found the following article on MSDN that explains how to resolve conflicts using the RefreshMode:
http://msdn.microsoft.com/en-us/library/system.data.linq.refreshmode.aspx
I decided KeepCurrentValues makes sense for my requirement and I also found this page with an example for this mode:
http://msdn.microsoft.com/en-us/library/bb399421.aspx
However when I implemented this the values from the database always overwrote my changes. I tried changing the RefreshMode to OverwriteCurrentValues and KeepChanges and each time the values from the database were saved. My approach was to manually change values in the database while in debug mode in VisualStudio. The code in VS is:
[MyDataContext] db = new [MyDataContext]();
try
{
[MyLINQType] old = (from o in db.[MyLINQType] where o.ID=1 select o).Single();
old.IntField = 55;
// This is where I change the IntField value manually in the database in
// debug mode to generate the conflict.
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
}
I know that the conflict appears but every time, no matter how I change the RefreshMode, the value 55 is never saved and the changes that I made manually in the database are kept. Is there some trick to achieve the desired result? I have tried generating the conflict from inside the code at first and that didn't work as expected either. Maybe I didn't understand how the RefreshMode should work. Any ideas are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要再次调用 SubmitChanges...
冲突解决方法确定 DataContext 的状态,而不是底层数据库的状态。
KeepCurrentValues 表示保留 DataContext 中当前的所有值,这意味着下次调用 SubmitChanges 将保存用户所做的任何更改,但它也会覆盖当前用户加载数据后其他用户所做的任何更改。
KeepChanges 表示仅保留自加载到 DataContext 以来已更改的值,这意味着下次调用 SubmitChanges 将保存用户所做的任何更改,并将保留其他用户所做的任何更改。并且,如果另一个用户更改了与当前用户相同的值,则当前用户的更改将覆盖它。
OverwriteCurrentValues 意味着使用当前数据库值更新 DataContext,这意味着当前用户所做的所有更改都将被丢弃。
You just need to call SubmitChanges again...
The conflict resolution method determines the state of the DataContext and not the underlying database.
KeepCurrentValues means keep all values as they are currently in the DataContext, which means the next call to SubmitChanges will save any changes made by the user, but it will also overwrite any changes made by other users after the data was loaded by the current user.
KeepChanges means keep only the values that have been changed since being loaded into the DataContext, which means the next call to SubmitChanges will save any changes made by the user and will preserve any changes made by other users. And, if another user changed the same value as the current user, the current user's change will overwrite it.
OverwriteCurrentValues means update the DataContext with the current database values, which means that all changes made by the current user will be discarded.
在更改数据之前,您可以通过从 Db 覆盖来刷新对象:
Before changing data, you refresh your object by overwriting from Db :