How do you handle situations by which I want pre-existing data prior to the application is began or immediately after the database is produced. For instance, I've a listing of nations by which Let me load in to the database after code-first creates it. How do you do that?

Application is structured the following:

Repository > Service > WebMVC

The xml is incorporated in the WebMVC project.

You create custom initializer, which gets from DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways interface. Like:

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext->

And you overwrite Seed method like:

protected override void Seed(YourDbContext context)

Whole example might seem like:

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext>
    protected override void Seed(EntitiesContext context)
        List<Role> roles = new List<Role>
            new Role {Id=1, Title="Admin"},
            new Role {Id=2, Title="ProjectManager"},
            new Role {Id=3, Title="Developer"}

        // add data into context and save to db
        foreach (Role r in roles)


Edit: After setting this up, you need to setup Initializer too, as Ladislav Mrnka pointed out.

Database.SetInitializer(new EntitiesContextInitializer());

ie.: in Global.asax:

protected void Application_Start()
            Database.SetInitializer(new EntitiesContextInitializer());

Be sure to add using System.Data.entity;

You have to create custom database initializer derived for instance from DropCreateDatabaseIfModelChanges and fill data in overriden Seed method. Then you definitely must use Database.SetInitializer to create your brand-new initializer when application begins. Here is example (from CTP5) accustomed to create custom index within the database.

To have an example begin to see the new MVC / Entity Framework tutorial series at Both #1 and #4 show initializer classes.