EF代码优先或Model First或DB First? [英] EF code first or Model First or DB First?

查看:117
本文介绍了EF代码优先或Model First或DB First?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个新的MVC应用程序。哪个最好

EF Code First?

模特第一?

Db第一?



我没有固定的数据库设计,我可能会修改现有的表可能会添加新表。

所以哪一个适合我的要求

I'm gonna create a new MVC Application. which is best
EF Code First ?
Model First?
Db First ?

I don't have fixed Database Design ,i may modify existing table may add new table .
so which one is Suitable for my Requirement

推荐答案

检查第一次与模型/数据库优先 [ ^ ]根据比较,您最好确定哪种情况适合您的情况。
check Code-first vs Model/Database-first[^] based on the comparison you better decide which is bes for your case.


引用:

我没有固定的数据库设计,我可能会修改现有的表可能会添加新的表。

I don't have fixed Database Design ,i may modify existing table may add new table .



正如你在这里所说,我预计你已经有了一个数据库,将来你可能需要修改。所以我建议选择数据库第一种方法。您只需添加数据库连接字符串,然后从数据库更新模型。您不需要再次创建类,然后生成数据库,因为您已经拥有相同的数据库。每当您添加新表时: -

从数据库更新模型 - >检查添加表(表的名称)

然后,您需要构建一次解决方案,然后为.tt文件运行自定义工具,该文件包含数据库中表的自动生成的类。 />
每当你改变任何表时: -

从数据库更新模型 - >检查刷新选择表名

然后按照上面提到的相同程序。

实体框架数据库优先 [ ^ ]



如果您想重新创建一个新数据库,那么Code first方法可以帮助您。

实体框架代码优先 [ ^ ]



我希望这对Sir先生有所帮助。

谢谢


As you say here, I anticipate you already have a database and may be you need modification in there in future. So I would suggest to opt for Database first approach. As you just need to add your database connection strings and then Update your model from database. You donot need to create again the classes and then generate the database as you already have the same. Whenever you add a new Table :-
Update model from database-> Check Add table(name of the table)
Then you need to build your solution once and then run custom tool for the .tt file that containes the auto generated classes for your tables in the database.
Whenever you alter any table:-
Update model from database-> Check Refresh select the table name
Then the same procedure mentioned above.
Entity Framework Database First[^]

If you think of recreate a new database, then Code first approach would help you.
Entity Framework Code First[^]

I hope this helps Sir.
thanks


你好,



ADO .NET Entity Framework允许开发人员在三种可能的方法中选择任何一种方法:Database First,Model First和Code F. irst。



数据库优先:这是一个基于现有数据库的更加以数据为中心的设计。实体框架能够基于关系数据库中的表和列生成业务模型。有关我们的数据库结构(存储模式),我们的数据模型(概念模型)以及它们之间的映射的信息以XML格式存储在.edmx文件中。



模型优先:在这种方法中,我们没有现有的数据库,实体框架提供了一个可以创建概念数据模型的设计器。它还使用.edmx文件来存储模型和映射信息。创建模型后,Entity Framework设计器可以生成可用于创建数据库的数据库模式。



代码优先:无论您是否拥有现有数据库,您都可以编写自己的与表和列对应的类和属性,并将它们与没有.edmx文件的Entity Framework一起使用。在这种方法中,Entity Framework不利用任何类型的配置文件(.edmx文件)来存储数据库模式,因为映射API使用这些约定在运行时动态生成数据库模式。



我个人认为你应该采用代码优先方法,因为它用于快速开发,开发人员可以完全控制实体。你可以在这里查看我的意见: http://www.itworld.com/article/2700195/development/3-reasons-to-use-code-first-design-with-entity-framework.html [ ^ ]





这是一个完整的代码第一步骤系列一步一步



使用代码优先方法实体框架中的关系Fluent API [ ^ ]



使用实体框架进行代码优先迁移 [ ^ ]



在MVC中使用Entity Framework 5.0 Code First方法进行CRUD操作 [ ^ ]



使用MVC中的存储库模式进行CRUD操作 [ ^ ]



CRUD Oper使用MVC中的通用存储库模式和工作单元 [ ^ ]



使用MVC中的通用存储库模式和依赖注入的CRUD操作 [ ^ ]



谢谢
Hi,

The ADO.NET Entity Framework allows developers to choose any one approach among three possible approaches: Database First, Model First and Code First.

Database First: It is a more data-centric design that is based on an existing database. The Entity Framework is able to generate a business model based on the tables and columns in a relational database. The information about our database structure (store schema), our data model (conceptual model), and the mapping among them is stored in XML in an .edmx file.

Model First: In this approach, we don't have an existing database and the Entity Framework offers a designer that can create a conceptual data model. It also uses an .edmx file to store the model and mapping information. When the model has been created then the Entity Framework designer can generate the database schema that can be used to create the database.

Code First: Whether you have an existing database or not, you can code your own classes and properties that correspond to tables and columns and use them with Entity Framework without an .edmx file. In this approach Entity Framework does not leverage any kind of configuration file (.edmx file) to store the database schema, because the mapping API uses these conventions to generate the database schema dynamically at runtime.

My personal opinion you should go with code first approach because it is used to fast development and developer has full controls on entities. you can check here for my opinion : http://www.itworld.com/article/2700195/development/3-reasons-to-use-code-first-design-with-entity-framework.html[^]


Here is a complete series for code first approach step by step

Relationship in Entity Framework Using Code First Approach With Fluent API[^]

Code First Migrations With Entity Framework[^]

CRUD Operations Using Entity Framework 5.0 Code First Approach in MVC[^]

CRUD Operations Using the Repository Pattern in MVC[^]

CRUD Operations Using the Generic Repository Pattern and Unit of Work in MVC[^]

CRUD Operations Using the Generic Repository Pattern and Dependency Injection in MVC[^]

Thanks


这篇关于EF代码优先或Model First或DB First?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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