EDMX图中使用实体框架4.1代码优先优于模型/数据库优先的优点和缺点是什么?
我试图充分理解使用EF 4.1构建数据访问层的所有方法。我使用存储库模式和IoC。
我知道我可以使用代码优先的方法:手动定义实体和上下文,并使用ModelBuilder对模式进行微调。
我还可以创建一个EDMX图并选择一个代码生成步骤,该步骤使用T4模板来生成相同的POCO类。
在这两种情况下,我最终得到的POCO对象是ORM不可知的,而上下文则来自DbContext。
数据库优先似乎是最有吸引力的,因为我可以在企业管理器中设计数据库,快速同步模型并使用设计器对其进行微调。
那么这两种方法有什么不同呢?仅仅是VS2010 vs企业管理器的偏好问题吗?
代码优先似乎是后起之秀。我快速浏览了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类来映射现有数据库。
代码优先似乎是后起之秀。我快速浏览了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类来映射现有数据库。