Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EF 4.1如何将web服务返回的图形合并到现有上下文中_Entity Framework_Entity Framework 4.1 - Fatal编程技术网

Entity framework EF 4.1如何将web服务返回的图形合并到现有上下文中

Entity framework EF 4.1如何将web服务返回的图形合并到现有上下文中,entity-framework,entity-framework-4.1,Entity Framework,Entity Framework 4.1,我有一个场景,需要将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(newC

我有一个场景,需要将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)
如果我试图附加包含上下文中已存在的实体的对象,则会创建重复冲突

为避免此问题,我正在清除本地数据,然后在附加之前调用AcceptableChanges:

        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的清理工作吗


我无法在每次需要附加图表时销毁和创建新的上下文,因为它包含其他可能丢失的对象。

处理当前上下文并使用新上下文如何?如果从web服务接收的数据远远大于您已经持有的数据,是否最好将上述内容反转?创建新上下文,附加web服务数据,将该上下文用作起点(即,在重复检查后合并现有数据?)当您说“合并现有数据”时,您的意思是使用附加功能吗?如果是的话,我不知道这是怎么回事。附着对象也会附着所有相关对象。这意味着无论如何我都必须清除旧上下文中的大部分数据,我最终也会遇到同样的问题。