Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How DbMigrationsConfiguration is related to a DbMigration in EF

In Entity Framework by using Enable-Migrations a Migrations folder is created containing a Configuration inherited from DbMigrationsConfiguration like this:

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

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

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

    public override void Down()
    {
        ...
    }
}

I didn't find any code that relates these two together ( for example having a configuration property in migrations). The only relation I found is that both are placed in same folder. If I have more than 1 DbContext and consequently more than 1 Configuration, I'm wondering how these DbMigrations are distinguished?

Question: How DbMigration classes are related to a Configuration?

like image 972
mehrandvd Avatar asked Jul 07 '15 11:07

mehrandvd


1 Answers

They are related by convention. By default, it will store the migrations in a root folder called Migrations. You can override this in the constructor of the config (https://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrationsconfiguration(v=vs.113).aspx) or when you enable-migrations:

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

For multiple contexts, create a different config and folder for each by using -ContextTypeName ProjectName.Models.Context2 -MigrationsDirectory:Migrations\Context2. Here is a walkthrough: http://www.dotnettricks.com/learn/entityframework/entity-framework-6-code-first-migrations-with-multiple-data-contexts

like image 51
Steve Greene Avatar answered Sep 18 '22 05:09

Steve Greene