Entity Framework, automatic migration application

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?

+13
source share
3 answers

Finally, I found a solution to my problem. I call this method every time the application starts:

 public void InitializeDatabase(DataAccessManager context) { if (!context.Database.Exists() || !context.Database.CompatibleWithModel(false)) { var configuration = new DbMigrationsConfiguration(); var migrator = new DbMigrator(configuration); migrator.Configuration.TargetDatabase = new DbConnectionInfo(context.Database.Connection.ConnectionString, "System.Data.SqlClient"); var migrations = migrator.GetPendingMigrations(); if (migrations.Any()) { var scriptor = new MigratorScriptingDecorator(migrator); var script = scriptor.ScriptUpdate(null, migrations.Last()); if (!string.IsNullOrEmpty(script)) { context.Database.ExecuteSqlCommand(script); } } } } 
+15
source

Automatic migration means that you do not need to run the add-migration command for your changes in models, but you need to run the update-database command manually.

If automatic migration is turned on when you call update-database , if changes are expected in the models, "automatic" migration will be added, and the database will be updated.

If you want your database to be updated without having to call the update-database command, you can add the Database.SetInitializer(...) method in OnModelCreating() to your context, for example:

 public class MyContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MigrateDBConfiguration>()); } ... } public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<MyContext> { ... 

Note that you must declare DbMigrationsConfiguration and MigrateDatabaseToLatestVersion as your real context, and not the default DbContext .

+15
source

If you have changes to your entities, you first need to run add-migration to create a script migration.

After that in Global.asax

you need to have code like this

  var configuration = new MyProject.Configuration(); var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration); migrator.Update(); 

every time you run an asp.net project, it checks to see if you have a new migration to run and run update-database automatically for you.

+3
source

Source: https://habr.com/ru/post/1245922/


All Articles