实体框架自动迁移对迁移历史记录位置的影响

7

当自动迁移更新数据库时,我总是遇到以下错误。

不支持影响迁移历史记录系统表位置(例如默认模式更改)的自动迁移。请使用基于代码的迁移执行影响迁移历史记录系统表位置的操作。

这是我的代码:

[DbConfigurationType(typeof(AlvinCMSExtension.Migration.AlvinCMSCustomHistoryConfiguration))]
public class AccountDBContext : DbContext
{
    public AccountDBContext()
        : base("DefaultConnection")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<Alvin_CMS.Models.AccountDBContext, Alvin_CMS.Migrations.AccountDBContext.Configuration>());
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Membership> Memberships { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UsersInRole> UsersInRoles { get; set; }
    public DbSet<OAuthMembership> OAuthMemberships { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
        string query = "select schema_name()";

        if (con.State == ConnectionState.Closed)
            con.Open();

        SqlCommand com = new SqlCommand(query, con);
        var x = com.ExecuteScalar();

        if (con.State == ConnectionState.Open)
            con.Close();

        modelBuilder.HasDefaultSchema(x.ToString());
    }
}

internal sealed class Configuration : DbMigrationsConfiguration<Alvin_CMS.Models.AccountDBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        MigrationsDirectory = @"Migrations\AccountDBContext";
        //SetHistoryContextFactory("System.Data.SqlClient", (conn, schema) => new AccountHistoryContext(conn, schema));
    }

    protected override void Seed(Alvin_CMS.Models.AccountDBContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

public class CustomHistoryContext : HistoryContext
{
    public CustomHistoryContext(DbConnection dbConnection, string defaultSchema)
        : base(dbConnection, defaultSchema)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.HasDefaultSchema("dbo");
        modelBuilder.Entity<HistoryRow>().ToTable(tableName: "__MigrationHistory", schemaName: "dbo");
        //modelBuilder.Entity<HistoryRow>().ToTable(tableName: "__MigrationHistory", schemaName: "dbo");
    }
}

public class AlvinCMSCustomHistoryConfiguration : DbConfiguration
{
    public AlvinCMSCustomHistoryConfiguration()
    {
        this.SetHistoryContext("System.Data.SqlClient",
            (connection, defaultSchema) => new CustomHistoryContext(connection, "dbo"));
    }
}

我可以在其他DB上下文中轻松进行迁移,但仅在使用此AccountDBContext时才会发生错误。这个错误的原因是什么?

一个不好的解决方案是在数据库里没有重要内容时删除数据库,然后应用迁移。 - Adnan Umer
我在新创建的数据库上完成了这个操作,结果是一样的。 - Alvin Stefanus
1个回答

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接