简体   繁体   English

使用IdentityDbContext和Code First自动迁移表位置和架构的实体框架?

[英]Entity Framework using IdentityDbContext with Code First Automatic Migrations table location and schema?

I am trying to setup automatic migration updates using the IdentityDbContext class and propagating changes to the actual DbContext for the entire database. 我正在尝试使用IdentityDbContext类设置自动迁移更新,并将更改传播到整个数据库的实际DbContext。

Before I get into the code, on my implementation of the IdentityDbContext with automatic migrations I get this error: 在我进入代码之前,在我使用自动迁移实现IdentityDbContext时,我收到此错误:

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. 请对基于代码的迁移使用影响迁移历史记录系统表位置的操作。

I am not going to post the models that are associated with the migrations and context code unless someone finds them of use. 我不会发布与迁移和上下文代码关联的模型,除非有人发现它们的使用。

Implemenation of the 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");
        }
    }

So I tried adding this class to place the migrations history into the correct schema. 所以我尝试添加此类以将迁移历史记录放入正确的模式中。 This, in fact, does move the migrations history into the correct schema, but everything else remains in the dbo schema. 实际上,这确实将迁移历史记录移动到正确的模式中,但其他所有内容都保留在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)
        {
           ...
        }
    }

Ideally, I'd like everything to be in the ft schema. 理想情况下,我希望所有内容都在ft模式中。 I don't think the migrations are that complex that I need to manually setup the migrations. 我不认为迁移是我手动设置迁移所需的那么复杂。 I was hoping for simplicity sake, I could use automatic migrations for this. 我希望为了简单起见,我可以使用自动迁移。 I am wondering if this approach is impossible and what I need to do to make this happen and any changes made to the models do get propagated. 我想知道这种方法是不可能的,我需要做些什么来实现这一点,并且对模型所做的任何改变都会得到传播。

I have a similar issue with Oracle 12c and EF6: I cannot get automatic migrations to work. 我在Oracle 12c和EF6上遇到了类似的问题:我无法使自动迁移工作。 I found, however, the following partial success factors: - I needed to set 然而,我发现以下部分成功因素: - 我需要设置

modelBuilder.HasDefaultSchema("")

on my DbContext in order to get the runtime see the tables in the logon schema of the particular user - For the update-database it was necessary to set the MyHistoryContext parameters like that: 在我的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");
    }
}

NOTE: You need to hard-code the schema name there. 注意:您需要在那里硬编码模式名称。 In this way, update-database does not try to use dbo as schema (but still no automatic migrations are possible, they will drop your MigrationHistory table and mess up everything). 通过这种方式,update-database不会尝试使用dbo作为模式(但仍然不能进行自动迁移,它们会丢弃您的MigrationHistory表并弄乱所有内容)。 This is in my opinion a nasty bug inside either EF6 or the Oracle custom class. 在我看来,这是EF6或Oracle自定义类中的一个令人讨厌的错误。 As I have no maintenance contract with them, I cannot file a ticket. 由于我没有与他们签订维修合同,我无法提交机票。

For your case, I think its not possible somehow by design to avoid the error message with automatic migrations. 对于您的情况,我认为不可能以某种方式设计以避免自动迁移的错误消息。 EF6 thinks, for some reason, that if you use a custom schema name, that you actually moving the __MigrationHistory table from the default dbo schema, which is of course not true. 由于某种原因,EF6认为如果使用自定义模式名称,实际上是从默认的dbo模式移动__MigrationHistory表,这当然不是真的。

Or, did you find a solution for that? 或者,你找到了解决方案吗?

BR Florian BR弗洛里安

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

相关问题 独立于架构的实体框架代码优先迁移 - Schema independent Entity Framework Code First Migrations 使用Entity Framework代码优先迁移来更改数据库表名称 - Change a database table name using Entity Framework code first migrations 实体框架影响迁移历史记录位置的自动迁移 - Entity Framework Automatic migrations that affect the location of the migrations history 实体框架代码第一次迁移 - Entity Framework Code First Migrations 具有实体框架自动代码迁移的前缀表 - Prefix tables with entity framework automatic code migrations Entity Framework Core 2.1,使用代码优先迁移重命名表而不删除和创建新表 - Entity Framework Core 2.1, rename table using code-first migrations without dropping and creating new table 使用Entity Framework Code First迁移为大型查找表数据播种 - Seeding large lookup table data with Entity Framework Code First migrations 数据库实体框架代码中的额外表格首先使用数据库迁移 - Extra table in database Entity Framework Code First With Database Migrations 实体框架代码首先迁移多个数据库 - Entity Framework code first migrations with multiple databases 外键实体框架-代码优先迁移 - Foreign key Entity Framework - code first migrations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM