first vs Model/Database
What are the pros & cons of using Entity Framework 4.1 Code-first over Model/Database-first with EDMX diagram?
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
.
I know I can use code-first approach: define my entities and context by hand and use ModelBuilder
to fine-tune the schema.
I can also create an EDMX
diagram and choose a code generation step that uses T4 templates to generate the same POCO
classes.
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.
So what is the difference between those two approaches? Is it just about the preference VS2010 vs Enterprise Manager?
I think the differences are:
Code first
Database first
Model first
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.
I think this simple "decision tree" by Julie Lerman the author of "Programming Entity Framework" should help making the decision with more confidence:
More info Here.
Database first and model first has no real differences. Generated code are the same and you can combine this approaches. For example, you can create database using designer, than you can alter database using sql script and update your model.
When you using code first you can't alter model without recreation database and losing all data. IMHO, this limitation is very strict and does not allow to use code first in production. For now it is not truly usable.
Second minor disadvantage of code first is that model builder require privileges on master database. This doesn't affect you if you using SQL Server Compact database or if you control database server.
Advantage of code first is very clean and simple code. You have full control of this code and can easily modify and use it as your view model.
I can recommend to use code first approach when you creating simple standalone application without versioning and using modeldatabase first in projects that requires modification in production.
链接地址: http://www.djcxy.com/p/15976.html上一篇: 实体框架代码优先和SQL Azure连接
下一篇: 首先与型号/数据库