EF4:POCO,自我跟踪实体,POCO代理之间的差异 [英] EF4: Difference between POCO , Self Tracking Entities , POCO Proxies
问题描述
实际上,我正在使用Entity Framework 4.0和POCO(Repository Pattern)我在POCO中做了一些修改,并调用ObjectContext.Savechanges,然后将其反映到DB。
我的问题是,
- 由于没有跟踪数据库,上下文是否将更改保留到数据库?
- 上下文是否为POCO生成跟踪信息?
我正在使用的示例代码, / p>
IEFRepository< Category> catRepository = new EFRepository< Category>();
类别c = catRepository.FindOne< Category>(x => x.Name ==Paper);
c.Name =纸;
catRepository.SaveChanges(System.Data.Objects.SaveOptions.None);
自我跟踪实体不是POCO。相反,他们非常坚持认识。甚至比 EntityObject
实体。什么使它们独一无二的是即使没有附加到 ObjectContext
也可以跟踪更改。
纯正如你所说,POCO使得变更跟踪变得困难。真的,唯一可以做的是比较对象的快照。对象上下文有一个 DetectChanges
方法。
使用伪POCO代理,您真正拥有的是一种在编译时看起来像几乎像POCO的类型,并且像运行时的非POCO一样。我说几乎,因为在运行时你会得到一个实例,它是一个编译时类型的子类型。因此,您要跟踪更改的任何属性必须是非私有的和虚拟的。类似的限制适用于延迟加载。您可以在中阅读更多信息这一系列文章在ADO.NET团队博客上。
Can someone point me the difference between POCO , Self Tracking Entities , POCO Proxies?
Actually, I am working Entity Framework 4.0 and POCO(Repository Pattern) and whenever I do some changes in the POCO and call ObjectContext.Savechanges then it reflects to the DB. My question is,
- How does the Context persist the change to the DB since it is not tracked?
- Does the Context generates the tracking info on the fly for POCO?
Sample Code I am using,
IEFRepository<Category> catRepository = new EFRepository<Category>();
Category c = catRepository.FindOne<Category>(x => x.Name == "Paper");
c.Name = "Paper";
catRepository.SaveChanges(System.Data.Objects.SaveOptions.None);
Self tracking entities are not POCOs. On the contrary, they are very much persistence-aware. More so than EntityObject
entities, even. What makes them unique is the changes can be tracked even when they are not attached to an ObjectContext
.
"Pure" POCOs, as you say, make change tracking difficult. Really, the only thing you can do is compare snapshots of the object. The object context has a DetectChanges
method for this.
With a pseudo-POCO proxy, what you really have is a type which looks (almost) like a POCO at compile time and like a non-POCO at runtime. I say "almost" because at runtime you will get an instance which is a subtype of the compile-time type. Because of this, any properties for which you want to track changes must be non-private and virtual. Similar restrictions apply to lazy loading. You can read more about this in this series of articles on the ADO.NET team blog.
这篇关于EF4:POCO,自我跟踪实体,POCO代理之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!