Entity Framework Database Setup for Code First<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Entity Framework Database Setup for Code First

Posted on: May 22, 2014

This article is part of a series of Entity Framework articles. This first article is how to generate the database from mode classes.

First, let's define model classes that will be used across all articles.

1public class Person { public int Id { get; set; } public int Name { get; set; } public DateTime BirthDate { get; set; }
3public ICollection<Person> Friends { get; set; }
5public House Residence { set; get; } }
7public class House { public int Id { get; set; } public double Price { get; set; }
9public Address Address { get; set; }
11public ICollection<Person> Resident { get; set; } public Person Owner { get; set; } }
13public class Address { public string Street { get; set; } public int Number { get; set; } public string City { get; set; } }

But, first we will comment the Address class and all references to it. The reason is that we do not want for the moment to configure anything.

Then, we need to add Entity Framework to your project.

EntityFrameworkAddNugetReference 400x129

The last step is to tell Entity Framework to initialize the database. This will create the database and all tables defined by the DbContext.

1public static void BuildDatabase() { using (var context = new YourContext()) { context.Database.Initialize(true); } }

We could also add an Entity to the database to generate the database automatically. This would produce the same result.

1public static void BuildByAddingEntity() { using (var context = new YourContext()) { var stud = new Person() { Name = "Person1", BirthDate = new DateTime(1990, 01, 01) }; context.Persons.Add(stud); context.SaveChanges(); } }

The result is not very interesting because it creates something without the help of any connection string. This mean that the database name is generated with the context name and the namespace. For example, the code executed generate a database would be "EntityFrameworkTestConsole.DataAccessLayer.YourContext". To fix this issue, we need to define a connection string and than set it in the YourContext. It also has used the default Sql Server defined for your machine. In general, it is SQL Express. This is very restrictive.


If you setup a connection string with a name, for example you could use "DefaultConnection", this one will be used if set in the context.

The app.config is where the connection string can be defined.

1<configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\\v11.0;Initial Catalog=EntityFrameworkTestConsole;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
1public class YourContext : DbContext { public YourContext(): base("DefaultConnection") {
4public DbSet<Person> Persons { get; set; } public DbSet<House> Houses { get; set; } }

This generate the database with the name specified in the Catalog from the connection string. You could also specify directly the connection string into the base() but this will be a show stopper if you are building application that must change the server and database depending of the environment (dev, test, prod).


Entity Framework takes care of everything for you. Tables are created from your entity defined in your context. It also takes care of all primary keys and foreign keys for you.


So far, we have see that Entity Framework can generate the database from minimalist code. Only a context need to be defined. Of course, setting a connection string and associate the connection string name with the context give you more control to which server and database to connect Entity Framework.

You can find this code at GitHub. This version of code can be downloaded in a Zip file here.