EF4: Difference between POCO , Self Tracking Entities , POCO Proxies

◇◆丶佛笑我妖孽 提交于 2019-12-10 15:12:11

问题


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,

  1. How does the Context persist the change to the DB since it is not tracked?
  2. 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);

回答1:


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.



来源:https://stackoverflow.com/questions/3966797/ef4-difference-between-poco-self-tracking-entities-poco-proxies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!