EF 4:如何更新断开连接的数据集?
我正在使用 EF 4、POCO。任务如下:拥有一长串 DomainObject 类型的数据,需要从数据库中获取这些数据并在断开连接的模式下进行更新(添加、更新、删除操作)。稍后如何将更新后的集推送回数据库?假设自从我们加载数据以来,表没有发生并行更改。我知道 context.SaveChanges() 会进行更新,但问题是如何将所有更改从某种列表放回 DbSet,或者也许有一种方法可以在断开连接模式下直接使用 DbSet?谢谢!
I'm using EF 4, POCO. The task is the following: having a long set of data of DomainObject type that need to be taken from Database and updated (add, update, delete operations) in a disconnected mode. How can I push the updated set back to the database later on? Lets assume there were no parallel changes to the table since we load data. I know context.SaveChanges() do the update, but the question is how to put all the changes back to DbSet from kinda List, or maybe there is a way to work directly with DbSet in disconnected mode? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自跟踪实体是 EDMX + ObjectContext API 的功能,因此如果您的映射/代码依赖于 DbContext API(您提到的
DbSet
),则无法使用它们。自跟踪实体是变更集模式的实现(如旧数据集)。关于SO的一些参考:如果您不使用 Self - 跟踪实体 答案非常简单 - 您必须告诉 EF 您做了哪些更改。一旦使用分离的实体,您必须将实体附加回新的上下文,并手动准确地说出您做了什么=插入、更新、删除了什么以及您如何更改关系。此过程中的关键组件是 ObjectContext API 中的
ObjectStateManager
(context.ObjectStateManager
) 或 DbContext API 中的DbChangeTracker
(context.ChangeTracker< /代码>)。在处理关系时,这是非常困难的,我们很多人通常使用另一个过程:我们再次加载实体图并将更改从分离的实体图合并到附加的实体图。一些参考:
请注意,根据您想要执行的更新数量,整个过程可能会非常慢。原因是 EF 不支持命令批处理,因此每个插入、更新、删除都是在数据库的单独往返中执行的。如果您更新 100.000 个复杂对象,则
SaveChanges
的执行可能需要几分钟时间。编辑:
在一些非常特殊的场景中,您仅使用没有关系的实体,您可以使用一种技巧来传输有关“更改”的信息:
这仅适用于具有简单键的平面对象。
Self-tracking entities are feature of EDMX + ObjectContext API so if you have your mapping / code dependent on DbContext API (you mentioned
DbSet
) you can't use them. Self-tracking entities are implementation of change set pattern (like old data set). Some reference on SO:If you don't use Self-tracking entities the answer is pretty simple - you must say EF what changes you did. Once using detached entities you must attach entities back to a new context and manually say exactly what you did = what is inserted, updated, deleted and how did you change relations. The key component in this process is
ObjectStateManager
in ObjectContext API (context.ObjectStateManager
) orDbChangeTracker
in DbContext API (context.ChangeTracker
). This is extremely hard when dealing with relation so many of us usually using another process: we load the entity graph again and merge changes from detached entity graph to attached entity graph. Some references:Be aware that whole your process can be pretty slow depending on number of updates you want to do. The reason is that EF doesn't support command batching so each insert, update, delete is executed in a separate round trip to the database. If you update 100.000 complex objects you can expect that execution of
SaveChanges
takes several minutes.Edit:
In some very special scenarios where you work only with entities without relations you can use a trick to transfer information about "changes":
This works only for flat objects with simple key.
您尝试过自我跟踪实体吗?
http://msdn.microsoft.com/en-us/library/ff407090.aspx
http://blogs.msdn.com/b/adonet/archive/2010/06/02/working-with-sets-of-self-tracking-entities.aspx
http://msdn.microsoft.com/en-us/library/ee789839.aspx#Y2300
http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx
Have you tried self-tracking entities ?
http://msdn.microsoft.com/en-us/library/ff407090.aspx
http://blogs.msdn.com/b/adonet/archive/2010/06/02/working-with-sets-of-self-tracking-entities.aspx
http://msdn.microsoft.com/en-us/library/ee789839.aspx#Y2300
http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx