Aviran Cohen
Aviran Cohen

Reputation: 5691

Entity Framework Update Database On Model Change

I am currently using auto-migrations with the following powershell console commands: add-migration update-database

I would like to know if there is an option to update the database automatically if the model changes (DbContext) without using the console commands.

Thanks.

Upvotes: 1

Views: 2670

Answers (1)

Steven V
Steven V

Reputation: 16585

I use

public class DataContext : DbContext
{

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext,Configuration>());

        base.OnModelCreating(modelBuilder);
    }
}

This causes the database migrations to the latest version when the data context is created at code runtime.

Upvotes: 1

Related Questions