问题描述
在Entity Framework中,通过使用 Enable-Migrations
创建一个 Migrations 文件夹,其中包含 Configuration
继承自 DbMigrationsConfiguration
,像这样:
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>
{
...
}
所有已创建的迁移使用 Add-Migration
创建的文件也放置在 Migrations 文件夹中。
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()
{
...
}
}
我没有找到任何将这两者联系在一起的代码(例如,在迁移中具有配置属性)。我发现的唯一关系是两者都放在同一文件夹中。如果我有1个以上的 DbContext
,因此有1个以上的配置,我想知道如何区分这些 DbMigration
?
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 DbMigration
s are distinguished?
问题: DbMigration
类与配置?
推荐答案
它们按照惯例相互关联。默认情况下,它将迁移存储在名为迁移的根文件夹中。您可以在配置的构造函数(),或者在您enable-migrations:
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";
}
对于多个上下文,请使用-ContextTypeName为每个上下文创建不同的配置和文件夹ProjectName.Models.Context2 -MigrationsDirectory:迁移\Context2。这是一个演练:
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
这篇关于DbMigrationsConfiguration与EF中的DbMigration有何关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!