实体可以从实体框架的ObjectStateManager中卸载吗

实体可以从实体框架的ObjectStateManager中卸载吗

本文介绍了一个实体可以从实体框架的ObjectStateManager中卸载吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WCF上使用具有自我跟踪实体的实体框架。

I am using Entity Framework with Self-Tracking Entities over WCF.

我有两个实体,客户和PriceMatrix具有多对多关系(客户可以拥有许多历史价格矩阵,矩阵可以在一个组之间共享的客户)

I have 2 entities, Customer and PriceMatrix with a many-to-many relationship (the customer can have many historic price matrices, and a matrix can be shared between a group of customers)

客户端有一个PriceMatrix,它更新并发送回服务器。服务器需要检查这个PriceMatrix是否在其他客户之间共享,然后再选择更新它,或复制它并更新副本。

The client has a single PriceMatrix which it updates and sends back to the server. The server needs to check if this PriceMatrix is shared among other customers before choosing to either update it, or duplicate it and update the duplicate.

我遇到的问题是因为我正在加载PriceMatrix以检查它,然后再附加更新的PriceMatrix当我调用ApplyChanges()时,我收到以下错误。

The problem I have is that because I am loading the PriceMatrix to examine it before I attach the updated PriceMatrix I am getting the following error when I call ApplyChanges().

如果我注释了我的支票(因此不加载PriceMatrix)一切都正常。

If I comment out my checks (and consequently don't load the PriceMatrix) everything works fine.

我真正想要做的是,在检查其状态后,卸载PriceMatrix,然后附加从客户端收到的更新的PriceMatrix。

What I really want to do is, after examining its state, unload the PriceMatrix and then attach the updated PriceMatrix I received from the client.

推荐答案

在这种情况下,请尝试使用 ObjectContext Detach 方法。这是在STEs - AcceptChanges 要求具有相同密钥的STE先前未加载到上下文中。

In such case try to use Detach method of the ObjectContext. This is known issue in STEs - AcceptChanges requires that STE with the same key is not previously loaded into context.

这篇关于一个实体可以从实体框架的ObjectStateManager中卸载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 19:37