如何解决EF 4.1如何将Web服务返回的图合并到现有上下文中
| 我有一种情况,需要将Web服务返回的大图(超过2000个实体)合并到现有的DbContext中。所以,我第一次尝试: \' get new object from web service
Dim newCust = DAL.GetCustomer(ActiveCustomerView.CustomerID)
\' attach the object graph into existing context
context.Customers.Attach(newCust)
万一我尝试附加包含上下文中已经存在的实体的对象,这会产生重复的冲突。
为避免此问题,我将清除本地数据,然后在附加之前调用AcceptAllChanges:
context.Orders.Local.Clear()
context.OrderItems.Local.Clear()
context.Messages.Local.Clear()
context.Contacts.Local.Clear()
context.Parcels.Local.Clear()
context.CustomerTransactions.Local.Clear()
context.ReturnedParcels.Local.Clear()
context.ReturnedOrderItems.Local.Clear()
context.InventoryItems.Local.Clear()
context.Shippments.Local.Clear()
context.Prints.Local.Clear()
DirectCast(context,IObjectContextAdapter).ObjectContext().AcceptAllChanges()
这可以工作,但是性能不可接受,因为清除数据需要花费40秒以上的时间。
还有其他有效方法吗?
我可以以某种方式处置DbSet清除它们的状态吗?
每次需要附加图形时,我都无法销毁并创建新的上下文,因为它包含可能丢失的其他对象。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。