How DbMigrationsConfiguration is related to DbMigration in EF

In the Entity Framework, using Enable-Migrations creates a Migrations folder containing the Configuration inherited from the DbMigrationsConfiguration , like this:

 internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext> { ... } 

All created migrations created using Add-Migration are also placed in the Migrations folder.

 public partial class Init: DbMigration { public override void Up() { ... } public override void Down() { ... } } 

I did not find the code that binds the two together (for example, having a configuration property during migration). The only thing I found is that both of them are placed in the same folder. If I have more than 1 DbContext and therefore more than 1 configuration, I wonder how these DbMigration differ?

Question: How are the DbMigration classes related to Configuration ?

+7
c # entity-framework code-first-migrations
source share
2 answers

They are bound by agreement. By default, it will store migrations in the root folder named Migrations. You can override this in the configuration constructor ( https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration(v=vs.113).aspx ) or when enabling-migration:

 public Configuration() { AutomaticMigrationsEnabled = true; MigrationsDirectory = @"Migrations\Context1"; } 

In several contexts, create a folder for each other configuration using -ContextTypeName ProjectName.Models.Context2 -MigrationsDirectory: Migrations \ Context2. Here's a walkthrough: http://www.dotnettricks.com/learn/entityframework/entity-framework-6-code-first-migrations-with-multiple-data-contexts

+6
source share

When you run the update-database command, database operations are performed in the up () method in the latest DbMigration classes. If successful, the commands in the Configuration class are executed. One of these methods is the seed () method, where you can optionally add code to insert values ​​into your tables after migration. When you specify a target migration (presumably earlier than the last), the migration works through the chain of down () methods in the migration classes to get to the version you need.

-one
source share

All Articles