Code-first vs Model / Database-first [英] Code-first vs Model/Database-first

查看:115
本文介绍了Code-first vs Model / Database-first的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是利弊&使用实体框架4.1的代码首先在模型/数据库首先与EDMX图?

我正在努力完全了解所有的方法来构建数据访问层使用EF 4.1。我使用Repository模式和 IoC

I'm trying to fully understand all the approaches to building data access layer using EF 4.1. I'm using Repository pattern and IoC.

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

I know I can use code-first approach: define my entities and context by hand and use ModelBuilder to fine-tune the schema.

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

I can also create an EDMX diagram and choose a code generation step that uses T4 templates to generate the same POCO classes.

在这两种情况下,我都会得到 POCO 对象,它们是 ORM 不可知和上下文源于 DbContext

In both cases I end up with POCO object which are ORM agnostic and context that derives from DbContext.

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

Database-first seems to be most appealing since I can design database in Enterprise Manager, quickly synch the model and fine-tune it using the designer.

那么这两种方法有什么区别?这是关于VS2010与企业管理员的偏好吗?

So what is the difference between those two approaches? Is it just about the preference VS2010 vs Enterprise Manager?

推荐答案

我认为差异是:

代码首先


  • 非常受欢迎,因为核心程序员不喜欢任何类型设计师和EDMX xml中定义的映射太复杂了。

  • 完全控制代码(没有自动生成的代码很难修改)。

  • 一般期望是不要烦恼DB。 DB只是一个没有逻辑的存储。 EF将处理创建,您不想知道该如何工作。

  • 由于您的代码定义了数据库,手工更改数据库将很可能会丢失。

  • 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.

数据库首先


  • 如果您拥有由DBA设计的DB,单独开发,或者如果您有现有的数据库,则受欢迎。

  • 您将让EF为您创建实体,并在映射修改后生成POCO实体。

  • 如果您想要POCO实体中的其他功能必须通过T4修改模板或使用部分类。

  • 数据库的手动更改是可能的,因为数据库定义了您的域模型。您可以随时从数据库更新模型(此功能非常的好)。

  • 我经常在VS数据库项目中使用(仅限Premium和Ultimate版本)。

  • 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).

模特儿


  • 是设计师风扇(=你不喜欢编写代码或SQL)。

  • 您将绘制您的模型,让工作流生成您的数据库脚本,T4模板生成您的POCO实体。您将失去对您的实体和数据库的一部分控制权,但是对于小型简单的项目,您将获得非常高的效率。

  • 如果您想要POCO实体中的其他功能,您必须通过T4修改模板或使用部分类。

  • 由于您的模型定义了数据库,因此手动更改数据库将最有可能丢失。如果您安装了数据库生成电源组件,这会更好。它将允许您更新数据库模式(而不是重新创建)或更新VS中的数据库项目。

  • 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的情况下还有几个其他功能与Code First vs. Model / Database相关。 Code Code中使用的流畅API不能提供EDMX的所有功能。我希望存储过程映射,查询视图,定义视图等功能首先使用模型/数据库时起作用,并且 DbContext (我还没有尝试),但是他们不要在代码中。

I expect that in case of EF 4.1 there are several other features related to Code First vs. Model/Database first. Fluent API used in Code first doesn't offer all features of EDMX. I expect that features like stored procedures mapping, query views, defining views etc. works when using Model/Database first and DbContext (I didn't try it yet) but they don't in Code first.

这篇关于Code-first vs Model / Database-first的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆