EF 4:使用POCO时无法理解DetectChanges(无自跟踪ObjectContext) [英] EF 4: Problems understanding DetectChanges when using POCO (no self tracking ObjectContext)

查看:93
本文介绍了EF 4:使用POCO时无法理解DetectChanges(无自跟踪ObjectContext)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wonder if anyone can help me?

我在理解为什么需要在我的POCO(非代理)实体上发出DetectChanges时遇到问题.

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

进行一些研究,如果我需要检查对象的状态",那么我需要发出detechChanges,但是为什么我需要检查对象的状态?

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?

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

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)

因此,我在理解为什么需要跟踪ObjectContext或了解更改方面遇到问题吗?

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?

也许我很想念消息,但是似乎如果我使用现有的ObjectContext(不是我每次都在创建和销毁它),确保ObjectContext知道将是有益的,否则不是吗?

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?

所以在1种方法中,我通过创建一个新的datacontext来更新对象,将其保存到db中并销毁ObjectContext.因此,我没有使用2种方法,一种是发送更新或新记录的方法,另一种是SAVING的方法.

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?

预先感谢

推荐答案

您的问题有点令人困惑.您正在撰写有关Entity Framework的文章,但使用的是与LinqToSql相关的DataContext.

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

该行为在您使用ObjectContext的方式上有所不同.从数据库加载POCO实体时,ObjectContext将其实例保存在内部Identity Map中.默认情况下,POCO不使用任何类型的变更跟踪.当您将该POCO实体保存到ObjectContext的相同实例时,它会在内部调用

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.

在您的方案中,您不使用ObjectContext的同一实例.在这种情况下,您首先必须将POCO实体附加到ObjectContext. MSDN 严格说,在附加时实体,将其标记为不变".因此,您必须说ObjectContext该实体已更改.您可以为整个实体,或者您可以确切地定义缺点).

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).

这篇关于EF 4:使用POCO时无法理解DetectChanges(无自跟踪ObjectContext)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆