EDMX图中使用实体框架4.1代码优先优于模型/数据库优先的优点和缺点是什么?

我试图充分理解使用EF 4.1构建数据访问层的所有方法。我使用存储库模式和IoC。

我知道我可以使用代码优先的方法:手动定义实体和上下文,并使用ModelBuilder对模式进行微调。

我还可以创建一个EDMX图并选择一个代码生成步骤,该步骤使用T4模板来生成相同的POCO类。

在这两种情况下,我最终得到的POCO对象是ORM不可知的,而上下文则来自DbContext。

数据库优先似乎是最有吸引力的,因为我可以在企业管理器中设计数据库,快速同步模型并使用设计器对其进行微调。

那么这两种方法有什么不同呢?仅仅是VS2010 vs企业管理器的偏好问题吗?


当前回答

数据库优先方法示例:

无需编写任何代码: ASP。数据库优先/数据库优先

我认为它比其他方法更好,因为这种方法数据丢失更少。

其他回答

数据库优先方法示例:

无需编写任何代码: ASP。数据库优先/数据库优先

我认为它比其他方法更好,因为这种方法数据丢失更少。

数据库优先和模型优先没有真正的区别。 生成的代码是相同的,您可以结合这两种方法。例如,您可以使用设计器创建数据库,也可以使用sql脚本更改数据库并更新模型。

当你首先使用代码时,你不能在没有重新创建数据库和丢失所有数据的情况下改变模型。恕我直言,这个限制非常严格,不允许在生产中首先使用代码。目前它还不能真正使用。

代码的第二个次要缺点首先是模型构建器需要对主数据库的特权。如果您使用SQL Server Compact数据库或控制数据库服务器,这不会影响您。

代码的优点首先是非常干净和简单的代码。您可以完全控制这些代码,并可以轻松地修改和使用它作为您的视图模型。

我建议使用代码优先的方法,当你创建简单的独立应用程序,没有版本,并使用模型\数据库优先的项目,需要在生产中修改。

我认为代码优先的优点之一是,你可以备份你对版本控制系统(如Git)所做的所有更改。因为所有的表和关系都存储在本质上只是类中,所以您可以回顾过去,看看数据库以前的结构是什么。

引用http://www.itworld.com/development/405005/3-reasons-use-code-first-design-entity-framework的相关部分

3 reasons to use code first design with Entity Framework 1) Less cruft, less bloat Using an existing database to generate a .edmx model file and the associated code models results in a giant pile of auto generated code. You’re implored never to touch these generated files lest you break something, or your changes get overwritten on the next generation. The context and initializer are jammed together in this mess as well. When you need to add functionality to your generated models, like a calculated read only property, you need to extend the model class. This ends up being a requirement for almost every model and you end up with an extension for everything. With code first your hand coded models become your database. The exact files that you’re building are what generate the database design. There are no additional files and there is no need to create a class extension when you want to add properties or whatever else that the database doesn't need to know about. You can just add them into the same class as long as you follow the proper syntax. Heck, you can even generate a Model.edmx file to visualize your code if you want. 2) Greater Control When you go DB first, you’re at the mercy of what gets generated for your models for use in your application. Occasionally the naming convention is undesirable. Sometimes the relationships and associations aren't quite what you want. Other times non transient relationships with lazy loading wreak havoc on your API responses. While there is almost always a solution for model generation problems you might run into, going code first gives you complete and fine grained control from the get go. You can control every aspect of both your code models and your database design from the comfort of your business object. You can precisely specify relationships, constraints, and associations. You can simultaneously set property character limits and database column sizes. You can specify which related collections are to be eager loaded, or not be serialized at all. In short, you are responsible for more stuff but you’re in full control of your app design. 3)Database Version Control This is a big one. Versioning databases is hard, but with code first and code first migrations, it’s much more effective. Because your database schema is fully based on your code models, by version controlling your source code you're helping to version your database. You’re responsible for controlling your context initialization which can help you do things like seed fixed business data. You’re also responsible for creating code first migrations. When you first enable migrations, a configuration class and an initial migration are generated. The initial migration is your current schema or your baseline v1.0. From that point on you will add migrations which are timestamped and labeled with a descriptor to help with ordering of versions. When you call add-migration from the package manager, a new migration file will be generated containing everything that has changed in your code model automatically in both an UP() and DOWN() function. The UP function applies the changes to the database, the DOWN function removes those same changes in the event you want to rollback. What’s more, you can edit these migration files to add additional changes such as new views, indexes, stored procedures, and whatever else. They will become a true versioning system for your database schema.

我首先使用EF数据库,以便对数据库配置提供更多的灵活性和控制。

EF代码优先,模型优先,起初看起来很酷,并且提供了数据库独立性,但是在这样做时,它不允许您指定我认为非常基本和常见的数据库配置信息。例如,表索引、安全元数据或主键包含多个列。我发现我想要使用这些和其他常见的数据库特性,因此必须直接进行一些数据库配置。

我发现DB第一次生成的默认POCO类非常干净,但是缺少非常有用的数据注释属性或到存储过程的映射。我使用T4模板来克服这些限制。T4模板非常棒,特别是与您自己的元数据和部分类结合使用时。

模型首先似乎有很多潜力,但在复杂的数据库模式重构过程中给我带来了很多错误。不知道为什么。