问题描述
|
我有一种情况,需要将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 (将#修改为@)