Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Which is better with Entity Framework , code first or database first ?

Why and when is it better?

Thank you,
C#

Posted
Comments
Sergey Alexandrovich Kryukov 23-Jun-15 12:33pm    
First of all, think at this:

Imagine that one of the two approaches was better in absolutely all cases. As your question doesn't provide any specific detail, we really need to consider all possible cases. Now, if one approach would really be always the best, don't you think that nobody would use another one, so it would never be mentioned?

Are you getting the idea why your question makes no sense?

—SA

Code first is better when you want to write code and have a database generated that reflects that code. Database first is better when you want to design the database yourself and have the code reflect that database. So if comes down to how particular you are about the database and how important you feel its design is. I think most professionals will go with database first, even if just for the lack of hassle (when you do code first and your code changes, your database and all its data is destroyed).
 
Share this answer
 
Comments
Andy Lanng 23-Jun-15 12:11pm    
I entirely agree. Well put. 5*
Member 9168963 23-Jun-15 12:23pm    
Yes both are well put. I just don't see many examples on CodeProject database first why I asked.

Database first is better for updating and easier.

Sergey Alexandrovich Kryukov 23-Jun-15 12:33pm    
This is good an interesting, but first please look at my comment to the question.
—SA
F-ES Sitecore 23-Jun-15 12:36pm    
I ignored the "which is better" and really answered the "when is it better". As you say, neither is "better" than the other, the better one is the one that meets your requirements.
Sergey Alexandrovich Kryukov 23-Jun-15 12:42pm    
Agree, a 5. I just think that the absurdity of the question should really be mentioned, to dismiss some inquirer's illusions.
—SA
For Larger projects that depend on an efficient db design:
(this is 90% of the dbs I have written)

I recommend pen and paper :S

The process good of DB design goes through several iterations of normalization and de-normalization. I find it useful to organize my db way before I start writing it.

I would then write the database first. There are several reasons for this but the best one is that I will just drag-drop into the dbml.

I will usually do that one table at a time. Personally, I don't think that any links should cross over each other, so I do this to prove my case.


I have been designing dbs for well over a decade so I can visualize the end result to a huge extent when I am designing and implementing it. You may find it easier to start with the EF model first so that you can visualize it better, but I find making tweaks and changes on the DB much easier and quicker that editing it from the EF models.

For smaller dbs or where query and data efficiency is not a priority
(such as basic content websites or just to handle global settings for eg)

Well, whatever is convenient. It isn't worth spending too much time on the design. If you know what tables are needed then you can easily create the EF first.

If you are likely to edit the database then I would swing more to the db first approach, unless changes are minor.


When handing over a small project to a junior, it would be best to have the EF first approach as this is easy to implement for anyone who accesses your source.


That's just IMHO ^_^


EDIT: Updated with Sergeys comments in mind
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Jun-15 12:33pm    
This is good an interesting, but first please look at my comment to the question.
—SA
Andy Lanng 23-Jun-15 12:35pm    
Yeah, i see you point. I'll refine my answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900