gpt4 book ai didi

c# - 影响迁移历史位置的 Entity Framework 自动迁移

转载 作者:行者123 更新时间:2023-11-30 12:22:35 25 4
gpt4 key购买 nike

我的自动迁移在尝试更新数据库时一直给我这个错误。

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.

这是我的代码:

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

我可以在其他数据库上下文中毫无问题地进行迁移,但只有在这个 AccountDBContext 中才会出现错误。这个错误的原因是什么?

最佳答案

错误是因为您定义了自定义架构名称。如果您想使用自定义架构名称,则不能使用自动迁移。如果您想使用自动迁移,请移除 OnModelCreating 中对 HasDefaultSchema 的调用。

Here is如何启用代码优先迁移。

关于c# - 影响迁移历史位置的 Entity Framework 自动迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40480571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com