EF 4:使用 POCO 时理解 DetectChanges 时出现问题(无自跟踪 ObjectContext)

发布于 2024-09-27 05:19:14 字数 649 浏览 8 评论 0原文

我想知道是否有人可以帮助我?

我无法理解为什么我需要在 POCO(非代理)实体上发出 DetectChanges。

当然,我有这条线来确保代理不会被返回。

   context.ObjectStateManager.GetObjectStateEntry(order).State

做一些研究后发现,如果我需要检查对象的“状态”,那么我需要发出 detechChanges 但为什么我需要检查对象的状态?

基本上,我将 POCO 实体发送到将数据保存到新 ObjectContext 的方法(我在每个方法上创建和销毁 ObjectContext)

因此,我无法理解为什么我需要让 ObjectContext 跟踪或了解更改?

是不是因为不知道就无法得救呢?

也许我错过了消息,但似乎如果我使用现有的 ObjectContext(我不是,我每次都在创建和销毁),确保 ObjectContext 知道会是有益的,但否则不会?

因此,在第一种方法中,我通过创建新的数据上下文来更新对象,将其保存到数据库并销毁 ObjectContext 。因此,我没有使用两种方法,一种方法发送更新或新记录,然后使用另一种方法进行保存。

我真的很感激任何关于为什么需要它的快速解释?

提前致谢

I wonder if anyone can help me?

I am having problems understanding why i need to issues DetectChanges on my POCO (non proxy) entities.

Of course i have this line to ensure that proxies are not returned.

   context.ObjectStateManager.GetObjectStateEntry(order).State

And doing some research it appears if i need to check the "state" of an object then i need to issue detechChanges But why would i need to check the State of an object?

Basically I send along my POCO entity to a method that SAVES the data to a new ObjectContext (I create and destroy ObjectContext on each method)

Hence, i am having problems understanding why i would need to have ObjectContext track or be aware of changes?

Is it because that if its not aware if will not be saved?

Maybe i am miss informed but it appears that if i am using an existing ObjectContext (which i am not i am creating and destroying each time) that ensure ObjectContext is aware would be beneficial but otherwise not?

So in 1 method I am updating an object by creating a new datacontext, saving it to the db and destroying ObjectContext . Hence i am not using 2 methods, 1 method to send the update or new record and then another method for SAVING.

I would really appreciate any quick explaanations of why its needed?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-10-04 05:19:14

你的问题有点令人困惑。您正在撰写有关实体框架的文章,但使用与 LinqToSql 相关的 DataContext。

该行为因使用 ObjectContext 的方式而异。当您从数据库加载 POCO 实体时,ObjectContext 将其实例保存在内部身份映射中。默认情况下,POCO 不使用任何类型的更改跟踪。当您将该 POCO 实体保存到 ObjectContext 的同一实例时,它会在内部调用 DetectChanges 将当前实体状态与存储的状态进行比较。此比较定义了哪些列必须更新。对 DetectChanges 的内部调用是默认行为,可以将其关闭,因此您必须手动调用此方法。

在您的场景中,您不使用相同的 ObjectContext 实例。在这种情况下,您首先必须将 POCO 实体附加到 ObjectContext。 MSDN 严格说明在附加时实体它被标记为未更改。因此,您必须说 ObjectContext 该实体已更改。您可以对整个实体执行此操作< /a> 或者您可以准确定义哪个 属性已更改,但您必须手动执行 = 您必须将该信息存储在某处(自我跟踪实体可以帮助您,但它们还有其他缺点)。

Your question is little bit confusing. You are writting about Entity Framework but using DataContext which is related to LinqToSql.

The behavior differs in the way you are using ObjectContext. When you load POCO entity from database ObjectContext holds its instance in internal Identity Map. By default POCO doesn't use any kind of change tracking. When you save that POCO entity to the same instance of ObjectContext it internally calls DetectChanges to compare current entity state with stored state. This comparision defines which columns have to be updated. Internal call to DetectChanges is default behavior which can be turned off so you will have to call this method manually.

In your scenario you not using the same instance of ObjectContext. In that case you first have to Attach POCO entity to the ObjectContext. MSDN strictly says that when attaching entity it is marked as Unchanged. For that reason you have to say ObjectContext that entity has changed. You can do that for whole entity or you can define exactly which properties have changed but you have to do it manually = you have to store that information somewhere (Self tracking entities can help you with that but they have ohter disadvantages).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文