4

When using the Entity Framework there are basically two ways to create your model. You either create the model in SQL server or in Visual Studio EF designer. Those are outlined below.

Start with Database

You first create the model in your SQL server DB then point EF to create the .edmx file for you. By using this approach you can use SQL server management studio to create all of your models and relationships.

Start with Visual Studio EF Designer

This approach is to create the model first in Visual Studio and from that create your database. By doing this it seems like you don't have to be soo concerned with tables and relationships.

Here is what I do and why I do it that way

I start by creating my model using SQL server management studio. I do this because I think its easier to create and modify tables using that tool, also I know exactly what is being created. I create my EF model by pointing it to my existing database. After that I create a Visual Studio Database Project so that my database is scripted into files which I put into version control. When I need to make changes, I change the database and then update my .edmx file as well as my database project.

I was wondering what are the pros and cons to these different approaches and what should be the criteria to decide which to use? Am I doing it wrong? Should I be creating my model first in Visual Studio?

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
  • Here's _my_ approach to that: start *anywhere* and make changes *anywhere*. If one user prefer to update the model in VS and another prefer to make changes directly in the DB, let them do that. http://www.youtube.com/watch?v=doqYOlcEAZM – KristoferA Jun 09 '11 at 04:22
  • Thanks for the interesting read on the Database Project. Sounds like a nice solution to me. – Bart Verkoeijen Jun 15 '11 at 02:13
  • I always use a SQL Server database project, publish that to my sql server and then generate my edmx based on the schema in my database. – The Muffin Man Jul 07 '14 at 19:21

2 Answers2

2

I don't think that there's a 'right' or 'wrong' way to do this, a lot depends on how you deploy your code, where it goes to etc. There is also a third way, which Scott Guthrie blogged about recently:

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx


As a side note, even if you start with the model designer, I think you always have to think about your tables/relationships, as getting these wrong in the database can cause you big problems further down the line.

Paddy
  • 33,309
  • 15
  • 79
  • 114
0

I don't think there is a right or wrong way.

At our company we are developing the database changes directly first, apply them to the edmx model for existing models.
For new models, we create the edmx model first, then generate the database. From that point on we usually update the database directly. After we have tested our code internally and it runs correctly, and we know that our SQL database is correct (and of course prior to checking in), we'll then apply the changes to the database project by doing a SQL compare on the database to the database project.

This has worked very well for us.

irperez
  • 1,771
  • 2
  • 22
  • 26