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

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

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

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

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

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

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


当前回答

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

其他回答

我认为代码优先的优点之一是,你可以备份你对版本控制系统(如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.

我认为Julie Lerman(《Programming Entity Framework》的作者)所写的这棵简单的“决策树”能够帮助我们更自信地做出决定:

更多信息在这里。

依我之见,我认为所有的模型都有很好的地方,但我认为模型优先方法的问题是,在许多大型企业中,DBA控制数据库,如果不使用数据库优先方法,就无法获得构建应用程序的灵活性。我参与过许多项目,当涉及到部署时,他们想要完全控制。

因此,尽管我同意所有可能的变化,代码优先,模型优先,数据库优先,您必须考虑实际的生产环境。因此,如果您的系统将是一个拥有许多用户和DBA的大型用户基础应用程序,那么您可能会考虑数据库作为首选——这只是我的观点。

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

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

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

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