Click here to Skip to main content
Click here to Skip to main content

[EF] Initialize database with data using Code First

By , 16 Jun 2012
 

Introduction

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));
        
        context.SaveChanges();
    }
}

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.

History

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

License

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

About the Author

Nadege Deroussen
Architect
France France
Member
I'm coding in .Net since 8 years, most with asp.net and a little using WPF and MVC technology.
I learn 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 : http://nadege.deroussen.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1membermrrandom15 Jun '12 - 10:08 
short art.
GeneralRe: My vote of 1memberShahin Khorshidnia16 Jun '12 - 1:53 
Of course because it's a tip/trick and has been sent as Tip
If we don't know that we don't know, we'll stay double ignorant for ever.

AnswerRe: My vote of 1memberNadege Deroussen16 Jun '12 - 2:00 
As Shahin said, this is a tip and that's why it is short. But I have updated it and added links to my previous article about Entity Framework Code First. This should be published during the day.
By the way, do you find it helpful ? Do you thing I need to add some explanations ?
Thanks for your answer Smile | :)
Nadege Deroussen
Blog : http://nadege.deroussen.net

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 16 Jun 2012
Article Copyright 2012 by Nadege Deroussen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid