简体   繁体   English

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

[英]Entity Framework Automatic migrations that affect the location of the migrations history

My automatic migration keeps giving me this error when it tries to update the database. 我的自动迁移在尝试更新数据库时一直给我这个错误。

Automatic migrations that affect the location of the migrations history system table (such as default schema changes) are not supported. 不支持影响迁移历史记录系统表位置的自动迁移(例如默认架构更改)。 Please use code-based migrations for operations that affect the location of the migrations history system table. 请对基于代码的迁移使用影响迁移历史记录系统表位置的操作。

Here is my code: 这是我的代码:

[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"));
    }
}

I can do the migration without any problem with the other DB Context, but only with this AccountDBContext the error always occurs. 我可以在没有其他数据库上下文任何问题的情况下进行迁移,但只有使用此AccountDBContext时才会出现错误。 What is the cause of this error? 这个错误的原因是什么?

The error is because you have defined a custom schema name. 该错误是因为您已定义自定义架构名称。 If you want to use the custom schema name you can't use auto migrations. 如果要使用自定义架构名称,则无法使用自动迁移。 If you want to use auto migrations remove the call to HasDefaultSchema within OnModelCreating . 如果你想使用自动迁移删除调用HasDefaultSchemaOnModelCreating

Here is how you can enable Code First Migrations. 以下是启用Code First迁移的方法。

Execute Add-Migration command on Package manager console first and then Update-Database 首先在程序包管理器控制台上执行Add-Migration命令,然后在Update-Database中执行

ex: 例如:

add-migration <migration name>

update-database

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM