我对POCO的+实体框架v4的理解是否正确? [英] Is my understanding of POCO's + Entity Framework v4, correct?

查看:133
本文介绍了我对POCO的+实体框架v4的理解是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以通过实体框架v4来理解使用POCO吗?



如果我要使用POCO与我的EF4上下文,我还需要在设计器/ .edmx上放置/创建ENTITIES吗?不是使用POCO的想法,所以我不需要使用那些沉重的实体?或者我仍然需要这些实体,只是其他地方我实际上将数据从实体移动到我的POCO的...这是什么被任何消费代码使用?

解决方案

如果要使用POCO,您有三个选择:



首先是创建EDMX模型。在EDMX中,您将关闭代码生成,因此模型不会为您创建较重的实体。而您将创建必须遵循以下约束的POCO类:




  • 每个类必须与模型中的实体具有相同的名称, li>
  • 每个类都必须有无参数的构造函数。应该是公开的,但我认为它也适用于受保护的。

  • 每个类必须具有与模型中完全相同的名称的所有属性(包括导航属性)。除导航集合之外的所有属性都必须具有getter和setter(至少受到保护)。

  • 导航集合的属性必须至少为ICollection< T>的类型。你必须初始化它们(这就是为什么他们不需要setter)。在EF初始化集合时跟踪代理不是这种情况。



第二选择与第一选择相同,但不创建POCO自己上课相反,您可以使用可以下载到VS 2010的POCO模板。此模板使用.tt文件为您生成POCO。



第三选择是使用Code First方法编写您的POCO,并在代码中定义映射。为此,您需要 EF 4.0功能CTP 。我认为这是唯一的方式如何使用没有EDMX模型的POCO。但目前只有CTP有很多限制。


can someone please confirm/correct me, with my understanding of using POCO's with the Entity Framework v4?

If I'm going to want to use POCO's with my EF4 context, do I still need to place/create ENTITIES on the designer/.edmx ? Isn't the idea of using POCO's so I don't need to use those 'heavy' entities? or do i still need those entities, it's just that somewhere else I actually move the data out of the entities and into my POCO's .. which is what gets used by any consuming code?

解决方案

If you want to use POCO you have three choices:

First choice is to create EDMX model. In EDMX you will turn off code generation so the model will not create heavy entities for you. Than you will create your POCO classes which have to follow these constraints:

  • Each class has to have same name as entity in the model
  • Each class has to have parameterless constructor. Should be public but I think it also works with protected.
  • Each class has to have all properties (inclucing navigation properties) with exactly same names as in the model. All properties except navigation collections has to have getter and setter (at least protected).
  • Properties for navigation collections have to be at least type of ICollection<T> and you have to initialize them (that is the reason why they don't need setter). This is not the case for tracking proxies where EF initializes the collection.

Second choice is same as first but you don't create POCO classes by yourselves. Instead you use POCO template which can be downloaded to VS 2010. This template uses .tt file to generate POCOs for you.

Third choice is to use Code First approach where you code your POCOs and you define mapping in code. To do this you need EF 4.0 Feature CTP. I thik this is the only way how to use POCOs without EDMX model. But it is only CTP with many limitations at the moment.

这篇关于我对POCO的+实体框架v4的理解是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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