EF4:POCO、自跟踪实体、POCO 代理之间的区别
有人能指出 POCO、自我跟踪实体、POCO 代理之间的区别吗?
实际上,我正在使用 Entity Framework 4.0 和 POCO(存储库模式),每当我在 POCO 中进行一些更改并调用 ObjectContext.Savechanges 时,它都会反映到数据库。 我的问题是,
- 上下文如何将更改保存到数据库,因为它没有被跟踪?
- Context 是否会动态生成 POCO 的跟踪信息?
我正在使用的示例代码,
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自我跟踪实体不是 POCO。相反,他们非常有毅力意识。甚至比 EntityObject 实体还要多。它们的独特之处在于,即使未附加到 ObjectContext,也可以跟踪更改。
正如您所说,“纯粹的”POCO 使变更跟踪变得困难。实际上,您唯一能做的就是比较对象的快照。对象上下文有一个用于此目的的 DetectChanges 方法。
使用伪 POCO 代理,您真正拥有的是一种在编译时看起来(几乎)像 POCO 而在运行时看起来像非 POCO 的类型。我说“几乎”是因为在运行时您将获得一个实例,它是编译时类型的子类型。因此,您想要跟踪更改的任何属性都必须是非私有的和虚拟的。类似的限制也适用于延迟加载。您可以在 中阅读有关此内容的更多信息ADO.NET 团队博客上的本系列文章。
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 anObjectContext
."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.