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?
What is the Difference Between Code First and Database First Approach in MVC. The main difference between code first and database first approach in MVC is that the code first allows the programmer to create entity classes with properties first, and then create the database and tables based on the defined entity classes ...
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.
Code-first If chosen, it will create simple . cs file(s) which developers later modifies as per their requirement. Data-first If chosen, it will create a [name]. edmx file along with hierarchy of different files.
Code-First is mainly useful in Domain Driven Design. In the Code-First approach, you focus on the domain of your application and start creating classes for your domain entity rather than design your database first and then create the classes which match your database design.
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 haven't tried 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With