我正在尝试使用 IdentityDbContext 类设置自动迁移更新,并将更改传播到整个数据库的实际 DbContext。
在进入代码之前,在使用自动迁移实现 IdentityDbContext 时,我收到此错误:
影响迁移历史系统表位置的自动迁移(例如默认架构更改)不受影响
支持的。对于影响的操作,请使用基于代码的迁移
迁移历史系统表的位置。
我不会发布与迁移和上下文代码相关的模型,除非有人发现它们有用。
IdentityDbContext 的实现:
public class SecurityContext: IdentityDbContext<User>
{
public SecurityContext() : base("MyActualContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//removing this line I do not get an error, but everything gets placed into the dbo schema. Keeping this line, i get the above error.
modelBuilder.HasDefaultSchema("ft");
}
}
因此,我尝试添加此类以将迁移历史记录放入正确的架构中。事实上,这确实将迁移历史记录移至正确的架构中,但其他所有内容仍保留在 dbo 架构中。
public class MyHistoryContext : HistoryContext
{
public MyHistoryContext(DbConnection dbConnection, string defaultSchema)
: base(dbConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("ft");
}
}
public class SecurityContextMigrations : DbMigrationsConfiguration<SecurityContext>
{
public SecurityContextMigrations()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
//When the migrations get set, I use the new class created to move the migrations to the correct schema.
SetHistoryContextFactory("System.Data.SqlClient", (c, s) => new MyHistoryContext(c, s));
}
protected override void Seed(SecurityContext context)
{
...
}
}
理想情况下,我希望一切都在ft
架构。我不认为迁移那么复杂,我需要手动设置迁移。为了简单起见,我希望可以使用自动迁移来实现这一点。我想知道这种方法是否可行,我需要做什么才能实现这一点,以及对模型所做的任何更改都会传播。