开发者

Entity Framework 4 and Public Properties

I am working on a project and I am using Entity Framework 4 as my ORM. I am implementing POCO classes. Every example I see with EF 4 and POCOs implements all properties with public setters. I开发者_运维知识库s that the only way I can use POCO classes with EF 4? Do all my setters need to be public?


It depends how you use your entities.

"POCO" entities are kind of a lie (in any framework). True POCOs, which might have private or non-virtual state and no provision for serialization, can't do change-tracking. The only thing you can do with them in O/R mapping is materialize them.

So whey people talk about mapping "POCOs", there is usually some form of compromise to allow for change tracking. They're not really "POCOs"; they're "so-called POCOs."

One way to compromise is to make all persisted state public. Then you can do change tracking via snapshots.

Another way to compromise is to make all persisted state protected/virtual. Then you can do change tracking via proxies. It is not necessary for the properties to be public.

The EF doesn't support parameterized constructors (yet), so constructor injection (probably the best solution for your case with a "pure" POCO) isn't an option right now.


Use the POCO generator http://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313

http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx

they are virtual for change tracking (if that is what you want)

http://blogs.msdn.com/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜