44

How do I handle situations in which I need pre-existing data before the app is started or right after the database is generated. For example, I have a list of countries in which I'd like to load into the database after code-first generates it. How do I do this?

App is structured as follows:

Repository > Service > WebMVC

The xml is in the WebMVC project.

3 Answers 3

67

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

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext->

And then you overwrite Seed method like:

protected override void Seed(YourDbContext context)

Whole example might look 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)
        {
            context.Roles.Add(r);
        }
        context.SaveChanges();

    }
}

Edit: After setting this up, you have to set up Initializer too, as Ladislav Mrnka mentioned.

Database.SetInitializer(new EntitiesContextInitializer());

ie.: in Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    Database.SetInitializer(new EntitiesContextInitializer());
}

Don't forget to add using System.Data.Entity; .....

Sign up to request clarification or add additional context in comments.

9 Comments

This will only create list of roles. You must add them to the context and save changes.
@Dampe Can I use the Database.SetInitializer in the webUI project or does it have to be where I create the DbContext?
@Lol coder: You can add it into Global.asax file into Application_Start method. You will need to add Using.System.Data.Entity;
For some reason it executes the Database.SetInitializer method, but it does not execute the Seed method. I set break points in it and nothing happened.
Not a bad idea to call base.Seed() at the end of this method.
|
11

You must create custom database initializer derived for example from DropCreateDatabaseIfModelChanges and fill data in overriden Seed method. Then you must use Database.SetInitializer to set your new initializer when application starts. Here is example (from CTP5) used to create custom index in the database.

2 Comments

because I can only use the file access code from the web front end, is there a way for me to call the initializer or pass up the data instead?
You can wrap initialization in custom code and pass data from web app to that code which will in turn pass data to a new instance of your initializer.
1

For an example see the new MVC / Entity Framework tutorial series at http://www.asp.net/entity-framework/tutorials#Using%20MVC Both #1 and #4 show initializer classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.