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

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

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

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

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

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

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


当前回答

在SP1之前,使用大型机型的速度非常慢(SP1之后还没有尝试过,但据说现在很容易)。

我仍然先设计我的表,然后内部构建的工具为我生成poco,因此它承担了为每个poco对象执行重复任务的负担。

当你使用源代码控制系统时,你可以很容易地跟踪poco的历史,而使用设计器生成的代码就不那么容易了。

我的POCO有一个基础,这使得很多事情变得非常简单。

我有所有表的视图,每个基本视图为外键带来基本信息,我的视图POCO派生自我的POCO类,这是非常有用的。

最后,我不喜欢设计师。

其他回答

引用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.

代码优先似乎是后起之秀。我快速浏览了Ruby on Rails,他们的标准是代码优先,带有数据库迁移。

如果您正在构建一个MVC3应用程序,我认为Code首先具有以下优点:

Easy attribute decoration - You can decorate fields with validation, require, etc.. attributes, it's quite awkward with EF modelling No weird modelling errors - EF modelling often has weird errors, such as when you try to rename an association property, it needs to match the underlying meta-data - very inflexible. Not awkward to merge - When using code version control tools such as mercurial, merging .edmx files is a pain. You're a programmer used to C#, and there you are merging a .edmx. Not so with code-first. Contrast back to Code first and you have complete control without all the hidden complexities and unknowns to deal with. I recommend you use the Package Manager command line tool, don't even use the graphical tools to add a new controller to scaffold views. DB-Migrations - Then you can also Enable-Migrations. This is so powerful. You make changes to your model in code, and then the framework can keep track of schema changes, so you can seamlessly deploy upgrades, with schema versions automatically upgraded (and downgraded if required). (Not sure, but this probably does work with model-first too)

更新

这个问题还要求比较代码优先和EDMX模型/db优先。代码优先也可以用于以下两种方法:

模型优先:首先对poco编码(概念模型),然后生成数据库(迁移);或 数据库优先:给定一个现有数据库,手动对poco进行编码以匹配。(不同之处在于poco不是在现有数据库的情况下自动生成的)。您可以使用生成POCO类,并使用实体框架或实体框架5 -如何从现有数据库生成POCO类来映射现有数据库。

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

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

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

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

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

数据库优先方法示例:

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

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

在SP1之前,使用大型机型的速度非常慢(SP1之后还没有尝试过,但据说现在很容易)。

我仍然先设计我的表,然后内部构建的工具为我生成poco,因此它承担了为每个poco对象执行重复任务的负担。

当你使用源代码控制系统时,你可以很容易地跟踪poco的历史,而使用设计器生成的代码就不那么容易了。

我的POCO有一个基础,这使得很多事情变得非常简单。

我有所有表的视图,每个基本视图为外键带来基本信息,我的视图POCO派生自我的POCO类,这是非常有用的。

最后,我不喜欢设计师。