Click here to Skip to main content
13,357,219 members (73,774 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 15 Jun 2012

[EF] Initialize database with data using Code First

, 16 Jun 2012
Rate this:
Please Sign up or sign in to vote.
How to add initialization data when configuring Entity Framework Code First.


This article is a part of a series of articles I write about Entity Framework Code First. You can find my previous article here:

Now, if we know how to create a database using Code First, we could want to add some data during the database creation in order to have some useful data to test our interface. We can write a SQL script that will provide data in the database but EF Code First allows us to do it in an easy way, so let's try it. 

Using the code

The data would be defined in an initializer class:

public class EfInitializer : DropCreateDatabaseIfModelChanges<MyContext>
    protected override void Seed(MyContext context)
        var persons = new List<Person>
                    new Person {LastName = "Smith", FirstName = "John", BirthDate= DateTime.Now.AddDays(-1080)},
                    new Person {LastName = "Doe", FirstName = "James", BirthDate= DateTime.Now.AddDays(-1070)}
        persons.ForEach(p => context.Persons.Add(p));
        var projects = new List<Project>
                    new Project {Name = "Project 1", Manager = persons[0]},
                    new Project {Name = "Project 2", Manager = persons[0]},
                    new Project {Name = "Project 3", Manager = persons[1]}
        projects.ForEach(p => context.Projects.Add(p));

Once the data has been defined, we have to tell our context to initialize the database with our data. To do that, go in the Program.cs class and add the following line in the Main method:

Database.SetInitializer<MyContext>(new EfInitializer());

This line is really simple to understand. We are telling our database to use the class EfInitializer to initialize itself. Now you can run your console application and see that data is added to the database at startup. First we run the program:

Everything went fine. Let's see our database:

Good news: Our data is there!!!

Hope this will help you during your development.


  • June, 2012: First post.
  • June, 2012: Updated the post with links to my previous articles 


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


About the Author

Nadege Rouelle
France France
I'm coding in .Net since 9 years, most with ASP.Net and SharePoint and a little using WPF, MVC, Windows Phone 8 and WinRT technology.
I have learned so much reading others experience and tutorials, or tips so I try to do the same, keeping learning from others of course.
You can also find my blog here :

You may also be interested in...


Comments and Discussions

QuestionMore complex initialization Pin
snowburnt13-Aug-13 10:56
membersnowburnt13-Aug-13 10:56 
GeneralMy vote of 1 Pin
mrrandom15-Jun-12 11:08
membermrrandom15-Jun-12 11:08 
GeneralRe: My vote of 1 Pin
Shahin Khorshidnia16-Jun-12 2:53
memberShahin Khorshidnia16-Jun-12 2:53 
AnswerRe: My vote of 1 Pin
Nadege Deroussen16-Jun-12 3:00
memberNadege Deroussen16-Jun-12 3:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 16 Jun 2012
Article Copyright 2012 by Nadege Rouelle
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid