我正在尝试使用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
架构中。我不认为迁移是我手动设置迁移所需的那么复杂。我希望为了简单起见,我可以使用自动迁移。我想知道这种方法是否不可能以及我需要做些什么才能实现这一点,并且对模型所做的任何更改都会得到传播。
答案 0 :(得分:2)
我在Oracle 12c和EF6上遇到了类似的问题:我无法使自动迁移工作。然而,我发现以下部分成功因素: - 我需要设置
modelBuilder.HasDefaultSchema("")
在我的DbContext上,以便让运行时查看特定用户的登录模式中的表 - 对于update-database,有必要设置MyHistoryContext参数:
public class MyHistoryContext : HistoryContext
{
public MyHistoryContext(DbConnection dbConnection, string defaultSchema)
: base(dbConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("L2SRVRIZ");
}
}
注意:您需要在那里对架构名称进行硬编码。通过这种方式,update-database不会尝试使用dbo作为模式(但仍然不能进行自动迁移,它们会丢弃您的MigrationHistory表并弄乱所有内容)。在我看来,这是EF6或Oracle自定义类中的一个令人讨厌的错误。由于我没有与他们签订维护合同,我无法提交罚单。
对于您的情况,我认为不可能通过设计以避免自动迁移的错误消息。 EF6认为,出于某种原因,如果你使用自定义模式名称,你实际上是从默认的dbo模式中移动__MigrationHistory表,这当然不是真的。
或者,你找到了解决方案吗?
BR Florian