有没有可以直接从数据库生成代码的模板或工具?我想使用模型优先场景,但不想使用 .edmx 文件进行映射。有一个包含许多表的数据库,我不想为此编写所有类(我很懒)。那么,是否有一个模板可以从现有数据库自动生成代码并设置注释/使用流畅的 api 来定义关系等?
这在以下场景中也很有帮助。比如说,我将 .edmx 与 POCO 一起使用,现在我不需要 .edmx 文件中的映射。我想要代码中的映射。如果有一个工具或模板可以从现有数据库生成代码中的映射,那就太好了。
我开始学习 EF 4.1。我认为“在版本 2 中,代码优先成为模型优先,即在创建/发布数据库(在版本 1 中)之后,需要进行一些更改”。这是真的吗?我很想听听一些评论。谢谢。
Is there a template or tool to generate code from the database directly? I want to use model first scenario but do not want .edmx file for mappings. There is a database with many tables and I do not want to write all the classes (I am lazy) for that. So, is there a template to generate the code and set the annotations/use fluent api for defining the relationships, etc automatically from the existing database?
This would be helpful in the following scenario as well. Say, I was using .edmx with POCOs and now I do not want the mappings in the .edmx file. I want the mappings in the code. It would be great to have a tool or a template to generate the mappings in the code from the existing database.
I am starting on learning EF 4.1. I think "Code first becomes model first in version 2 i.e. after the database is created/released (in version 1) and needs some changes". Is that really true? I'd love to hear some comments. Thanks.
发布评论
评论(3)
查看 EF Power Tools CTP1。
Check out the 'Reverse Engineer Code First' feature of the EF Power Tools CTP1 that was just released.
为了生成类,您可以使用 POCO t4 模板生成。看看 此详细链接将帮助您入门。这样你就会得到生成的所有类。
对于映射,您可以对实体框架使用仅代码样式,但使用 POCO 模板生成类和上下文将比您自己创建映射具有更多优势。想象一下添加新表或修改表,这将涉及更多的工作。但我当然很想知道是否有任何绘图工具可以实现这一点。
For generating classes, you can use POCO t4 template generation. Have a look at this detailed link which will help you getting started. That way you will get all the classes generated.
For mapping, you can use Code-Only style for Entity Framework but generating classes and context using POCO template will have far more advantage over creating the mapping yourself. Imagine adding new tables or modifying the tables, it will involve more work. But I will certainly love to know if there is any mapping tool for that.
在使用 Oracle 数据库时,您可能正在使用适用于 Oracle 的 EF 提供程序 Devart dotConnect。在这种情况下,以下信息将帮助您选择工具。
Entity Framework Power Tools 的第一个版本还包含从现有数据库生成具有流畅映射的 Code-First 模型的功能。虽然很有用,但该功能在灵活性方面受到限制:开发人员只能设置连接字符串;接下来,从用户可用的所有数据库对象生成类。这并不是很方便,因为例如在 Oracle 中,用户可以使用包含数百甚至数千个表的大量模式。
Devart ADO.NET 提供程序的用户可以利用 Entity Developer(与 Devart 提供程序一起提供的 EF 设计器)极其强大的设计时开发功能,而不是求助于这种有限的功能。还可以选择数据库优先方法(如 EF Power Tools 中提供的)和模型优先方法(在 EDM 设计器中创建代码优先类)。
与 EF Power Tools 相比,用于开发 EF Code-First 模型的数据库优先方法还允许选择模型中必须可用的对象,设置生成类名和属性的命名规则等。此外,生成的模型可以在设计器中进行修改和改进。
为了更好地满足开发人员的需求,Entity Developer 中针对 C# 和 VB 的 Code-First 代码生成均基于 T4 模板,该模板易于访问,并且可以在 Entity Developer 中包含的功能丰富的 T4 编辑器中进行修改。
有关 Entity Developer 中 Code-First 开发的更多信息,请参阅“Entity Developer – EF Code First DbContext 模板”
http://www .devart.com/blogs/dotconnect/index.php/entity-developer-ef-code-first-dbcontext-template.html
It is possible that you are using EF-provider Devart dotConnect for Oracle when working with Oracle database. In this case the following information will help you to choose the tool.
The first version of Entity Framework Power Tools also contained the capability of generating a Code-First model with fluent mapping from an existing database. Although useful, this functionality is limited as regards its flexibility: the developer can only set the connection string; following that, classes are generated from all database objects available to the user. That is not extremely convenient, since in Oracle, for example, numerous schemas containing hundreds and sometimes thousands of tables are available to the user.
Rather than resort to this limited functionality, the users of Devart ADO.NET providers can avail themselves of impressively robust design-time development capabilities of Entity Developer, an EF-designer delivered with Devart providers. Also possible is the choice between the Database-First approach, as provided in EF Power Tools, and the Model-First approach, within which Code-First classes are created in the EDM-designer.
When compared to EF Power Tools, the Database-First approach to the development of EF Code-First models also allows selecting objects that must be available in the model, setting naming rules for the generation of class names and properties and so on. Besides, the resulting model can be modified and improved in the designer.
To better meet developers' needs, Code-First code generation in Entity Developer both for C# and VB is based on the T4-template that is easily accessible and can be modified in feature-rich T4 Editor contained in Entity Developer.
For more information on Code-First development in Entity Developer, see "Entity Developer – EF Code First DbContext Template"
http://www.devart.com/blogs/dotconnect/index.php/entity-developer-ef-code-first-dbcontext-template.html