简体   繁体   中英

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. 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 .

Here is how you can enable Code First Migrations.

Execute Add-Migration command on Package manager console first and then Update-Database

ex:

add-migration <migration name>

update-database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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