问题

什么是优点&使用Entity Framework 4.1的代码优先于模型/数据库优先与EDMX图?

我试图完全理解使用EF 4.1构建数据访问层的所有方法.我使用Repository模式和 IoC .

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

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

在这两种情况下,我最终得到 POCO 对象,它们是 ORM agnostic和从 DbContext 派生的上下文.

数据库首先似乎是最吸引人的,因为我可以在企业管理器中设计数据库,快速同步模型和使用设计器微调.

那么这两种方法有什么区别呢?是关于VS2010 vs企业管理器吗?



解决方法

我认为差异是:

先编码

  • Very popular because hardcore programmers don't like any kind of designers and defining mapping in EDMX xml is too complex.
  • Full control over the code (no autogenerated code which is hard to modify).
  • General expectation is that you do not bother with DB. DB is just a storage with no logic. EF will handle creation and you don't want to know how it does the job.
  • Manual changes to database will be most probably lost because your code defines the database.

数据库

  • Very popular if you have DB designed by DBAs, developed separately or if you have existing DB.
  • You will let EF create entities for you and after modification of mapping you will generate POCO entities.
  • If you want additional features in POCO entities you must either T4 modify template or use partial classes.
  • Manual changes to the database are possible because the database defines your domain model. You can always update model from database (this feature works quite good).
  • I often use this together VS Database projects (only Premium and Ultimate version).

首先建模

  • IMHO popular if you are designer fan (= you don't like writing code or SQL).
  • You will "draw" your model and let workflow generate your database script and T4 template generate your POCO entities. You will lose part of the control on both your entities and database but for small easy projects you will be very productive.
  • If you want additional features in POCO entities you must either T4 modify template or use partial classes.
  • Manual changes to database will be most probably lost because your model defines the database. This works better if you have Database generation power pack installed. It will allow you updating database schema (instead of recreating) or updating database projects in VS.

我预计在EF 4.1的情况下,还有其他几个与代码优先,模型/数据库相关的功能.代码中使用的Fluent API不提供EDMX的所有功能.我期望像存储过程映射,查询视图,定义视图等功能工作原理时,使用模型/数据库和 DbContext (我没有尝试它),但他们不在代码中.




相关问题推荐