Entity Framework and recreating the database when the model changes.

I’ve just started a new MVC5 project, and just couldn’t remember exactly how to do this.

So:

  1. Create a new project
  2. Create your models
  3. Create your DbContext
  4. Create a Database Initializer
  5. Add Database Initializer into Global.asax
public class ApplicationDbContextInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
    protected override void Seed(ApplicationDbContext context)
    {
        base.Seed(context);
    }
}
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbContextInitializer());

 

Advertisements

About John

Software developer, lately specialising in Full Stack Web Development using c#, ASP.Net WebAPI 2, and Angular.
This entry was posted in Development and tagged , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s