Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is advantage of CodeFirst over Database First?

I was watching some videos and tutorials for EF 4.1, and I do not understand any benefit of CodeFirst (except some if DB is very small 3-4 tables and I am lazy for creating DB first).

Mostly, the best approach so far is to create Database in some sort of Database editor, which is sure faster then editing in the Entity Model and EF picks up every relationships and creates associations correctly. I know there are challenges in naming convention etc, but I feel its very confusing to manage Code First as everything looks like code and its too much to code either.

What is it that CodeFirst can do and Db first cannot?

like image 718
Akash Kava Avatar asked Jul 02 '11 10:07

Akash Kava


People also ask

Why code First is better than database first?

3)Database Version Control 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.

Should I use code first or database first?

In this article, I will cover the two most popular approaches used by the programmers in domain driven applications. The main difference between Code First approach and Database First approach is that the Code First enables you to write entity classes and its properties first without creating the database design first.

What are the advantages of model first approach?

System Designers tend to prefer the Model First approach because it allows you to take into perspective the whole system and the relations amongst all the models and entities within the system without having to deal with how the data will eventually be stored or the technicalities of data annotations within the code.


2 Answers

CodeFirst cannot do anything that DB first cannot. At the end of the day they are both using the Entity Framework.

The main advantages of using codefirst are:

  • Development Speed - You do not have to worry about creating a DB you just start coding. Good for developers coming from a programming background without much DBA experience. It also has automatic database updates so whenever you model changes the DB is also automatically updated.
  • POCOs - The code is a lot cleaner you do not end up with loads of auto-generated code. You have full control of each of your classes.
  • Simple - you do not have a edmx model to update or maintain

For more info see Code-first vs Model/Database-first and here Code-First or Database-First, how to choose?

like image 200
Daveo Avatar answered Sep 30 '22 15:09

Daveo


Coming from a DataCentric approach, I will always find it strange the people like to create in a Code First Approach. When I design my database, I am already thinking about what each of the tables are as if they were classes anyway. How they link together and how the data will flow. I can image the whole system through the database.

I have always been taught that you work from the ground up, get your foundations right and everything else will follow. I create lots and lots of different systems for lots of different companies and the speed that I do it is based on the fact that once I have got a strong database model, I then run my custom code generator that creates the Views/Stored Procedures as well as my Controller/BusinessLayer/DataLayer for me, Put all of these together and all I have to do is create the front end.

If I had to create the whole system in code first to generate the database, as well as all of the other items then I would image it taking a lot longer. I am not saying that I am right in any terms, and I am sure that there are probably faster and more experienced ways of developing systems, but so far, I haven't found one.

Thanks for letting me speak and I hope my views have helped a little.

like image 44
user155140 Avatar answered Sep 30 '22 13:09

user155140