简体   繁体   English

使用实体框架T4 POCO

[英]Using of Entity Framework T4 POCO

I have been using Linq-to-SQL for a while to get access to my database. 我一直在使用Linq-to-SQL来访问我的数据库。 But I have recently been told this way of doing was not the best one since it allows to mix the data access & business logic layers. 但我最近被告知这种做法不是最好的,因为它允许混合数据访问和业务逻辑层。

I heard that Entity Framework T4 POCO was a solution but I cannot find complete information about it. 我听说Entity Framework T4 POCO是一个解决方案,但我无法找到有关它的完整信息。 Does anyone have more details to share with me ? 有没有人有更多细节与我分享?

Thanks in advance 提前致谢

What a POCO (Plain Old CLR Object) does it that it allows you to create your own representation class of your database. 什么是POCO(Plain Old CLR Object),它允许您创建自己的数据库表示类。 Entity Framework then converts your database (through a configuration (hint use an edmx file)) to the by you created POCO classes. 然后,实体框架将您的数据库(通过配置(提示使用edmx文件))转换为您创建的POCO类。

Example: 例:

Table User: 表用户:

id | fName | lName | otherField

You can represent this in your C# with a POCO to a user object with the following properties: 您可以使用POCO在C#中将此表示为具有以下属性的用户对象:

int id, string fName, string lName, var otherField.

Then you can, in the getters and setters of these properties, insert your business logic. 然后,您可以在这些属性的getter和setter中插入业务逻辑。

NOTE: I'd recommend using just the Entity Framework icm with an edmx file. 注意:我建议只使用带有edmx文件的Entity Framework icm。 And put your business logic somewhere else. 并将您的业务逻辑放在其他地方。 When creating a web service I always like the following order of classes : 创建Web服务时,我总是喜欢以下类的顺序:

  • A class that receives the calls and calls the right functions of the next class 接收调用并调用下一个类的正确函数的类
  • This class then converts the given params in the call into a format that the rest of the application understands and calls the right functions of another class. 然后,该类将调用中的给定参数转换为应用程序其余部分理解的格式,并调用另一个类的正确函数。
  • This class then checks the business logic in the params and calls another class to do something with the database. 然后,该类检查params中的业务逻辑,并调用另一个类对数据库执行某些操作。
  • This class then handles the database connection and stuff (with use of the Entity Framework) Note again: you can also use POCO's in this last step ;) 这个类然后处理数据库连接和东西(使用实体框架)再次注意:你也可以在最后一步使用POCO;)

I find this site gives a great example on how to use EF4 with POCO classes. 我发现这个网站提供了一个很好的例子,说明如何将EF4与POCO类一起使用。

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

This describes the 'code first' approach for Entity Framework 4 这描述了实体框架4的“代码优先”方法

See that: Entity Framework - Generating Classes 请参阅: 实体框架 - 生成类

There is a tutorial how to generate POCO Classes by existing database. 有一个教程如何通过现有数据库生成POCO类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM