自定义HistoryContext导致Get-Migrations失败

时间:2016-01-22 20:33:47

标签: c# oracle entity-framework

我正在使用Oracle的Entity Framework 12.1.22(6.121.2.0)和Entity Framework 6.0.0。

我有一个自定义的HistoryContext,可确保__MigrationHistory表与模型的其余部分位于同一模式中,因为我们有多个应用程序使用相同的数据库。

当我从头开始并从软件包管理器控制台运行Update-Database时,一切似乎都能正常工作,而__MigrationHistory表正好在我期望的位置。然而,很快就会出现问题:

PM> Update-Database -TargetMigration:0
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is already at version 0.

PM> Add-Migration 2
Unable to generate an explicit migration because the following explicit migrations are pending: [201601221958588_1]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

PM> Get-Migrations
Retrieving migrations that have been applied to the target database.
No migrations have been applied to the target database.

问题是Get-Migrations没有查找__MigrationHistory表的正确模式 - 它在用户模式中查找连接字符串中指定的用户。我通过在那里创建__MigrationHistory表的副本来验证这一点,此时所有上述命令都开始按预期工作。

有谁可以指出我做错了什么?

public class MyContext : DbContext
{
    public MyContext()
        : base("name=Blue")
    {
    }

    public virtual DbSet<MyEntity> MyEntities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.HasDefaultSchema("BLUE");
    }
}

public class MyEntity
{
    [Key, MaxLength(80)]
    public string Name { get; set; }
}

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        this.SetHistoryContext("Oracle.ManagedDataAccess.Client",
            (connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema));
    }
}

public class MyHistoryContext : HistoryContext
{
    public MyHistoryContext(DbConnection dbConnection, string defaultSchema)
        : base(dbConnection, defaultSchema)
    {
        this.defaultSchema = defaultSchema;
    }

    private string defaultSchema;

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.HasDefaultSchema(this.defaultSchema);
    }
}

我使用了这里的说明:Customizing the Migrations History Table (EF6 onwards)

0 个答案:

没有答案