I am using Entity Framework Code First with AutomaticMigrationsEnabled = true
:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext, MigrateDBConfiguration>()); ////////////////////////////////// public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DbContext> { public MigrateDBConfiguration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; } }
When you start the project for the first time, a database and tables are created, as expected. After changing my model by adding or removing fields, I launched Add-Migration
. The Migration class has been created, but an exception is thrown after starting the project:
An exception of type "System.InvalidOperationException" occurred in EntityFramework.dll, but was not processed in the user code
Additional information: the model supporting the DBContext context has been changed since the database was created.
UPDATE: Following the directions in the arturo menchaca answer, I changed my code as follows:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<DBContext, MigrateDBConfiguration<DBContext>>()); ...
After the change, an exception occurs:
The database already has an object named "MyTable".
How can I apply my database migration?
source share