Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What's the difference between code first and model first entity framework?

user-image
Question added by Mohammed Shaban , Senior software engineering , Thiqah
Date Posted: 2016/01/26
Sudheer Muhammed
by Sudheer Muhammed , Senior .Net Developer , ADNOC Logistics and Services

Code first

  • 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 do the job.
  • Manual changes to database will be most probably lost because your code defines the database.

Database first

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

 

 

 

Model first

  • IMHO popular if you are designer fan (= you don't like writing code or SQL).
  • You will "draw" your model and let workflow to generate your database script and T4 template to generate yout POCO entities. You will lose part of 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.

Monah Abou Hattoum
by Monah Abou Hattoum , Senior Software Developer , Specialized Medical Center (SMC)

There is no difference in functionalities, both generate databases and control the database version, but depending on the developer 's way of coding, if he prefers visual then he might find working with model first easier, if he prefers object oriented way, then code first is the best choice.In my case, I prefer the code first since I have full control over the code ( no auto-generated code like Model first), more powerful in case of adding polymorphism and inheritance...  the object oriented programming ideas.

Mohamed osama
by Mohamed osama , Microsoft .Net Developer , Prince Sattam Bin Abdul-Aziz University

Code First: 

write code and creat databse on the fly when the app run

 

Model First:

buld data base first before write code

Muhammad Abdul Majeed
by Muhammad Abdul Majeed , Full Stack .NET Developer , Abu Dhabi Ports

Code First: 

Well as it's already being depicted from the name that we first code according to which relationships and data model is generated.

 

Model FIrst:

Whereas in this approach we have to import the model according to which the classes are generated automatically. 

Jamil Abu naser
by Jamil Abu naser , Technical Team Lead , Tetco

Both of them used when you don't have Database, 

code first you will start write your own class and mapping between them then the database created based on that classes and when you run the application the entity framework will create the database for you, in other wise the model first you will create the edmx model inside the designer then you will export the script then you have to go to the database to run the script.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.