使用IdentityDbContext与$ C C首先自动迁移表的位置和模式$实体框架? [英] Entity Framework using IdentityDbContext with Code First Automatic Migrations table location and schema?

查看:1015
本文介绍了使用IdentityDbContext与$ C C首先自动迁移表的位置和模式$实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IdentityDbContext类和传播变为实际的DbContext整个数据库安装自动迁移更新。

I am trying to setup automatic migration updates using the IdentityDbContext class and propagating changes to the actual DbContext for the entire database.

在我进入code,我实现自动迁​​移的IdentityDbContext我得到这个错误:

Before I get into the code, on my implementation of the IdentityDbContext with automatic migrations I get this error:

影响移民的历史系统表的位置(如默认架构更改)自动迁移不   支持。请使用code型迁移的影响操作   在迁移历史系统表的位置。

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.

我不打算发布与该迁移和上下文code相关的车型,除非有人发现使用他们。

I am not going to post the models that are associated with the migrations and context code unless someone finds them of use.

在IdentityDbContext的实行:

Implemenation of the 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架构。

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.

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

推荐答案

我已经与Oracle 12c和EF6类似的问题:我不能自动迁移工作。然而,我发现,下面的部分成功因素: - 我需要设置

I have a similar issue with Oracle 12c and EF6: I cannot get automatic migrations to work. I found, however, the following partial success factors: - I needed to set

modelBuilder.HasDefaultSchema("")

我为了得到运行时看到表中的特定用户的登录模式的DbContext - 对于更新数据库,有必要设置MyHistoryContext参数这样:

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:

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

请注意:您需要硬$ C C存在的架构名称$。通过这种方式,更新数据库不会尝试使用DBO作为模式(但仍没有自动迁移是可能的,他们会删除您MigrationHistory表,乱了一切)。这是在我看来,无论是EF6或甲骨文自定义类中的讨厌的错误。当我与他们没有维护合同,我无法归档票。

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). This is in my opinion a nasty bug inside either EF6 or the Oracle custom class. As I have no maintenance contract with them, I cannot file a ticket.

有关你的情况,我认为它不可能以某种方式设计,以避免自动迁移的错误消息。 EF6认为,由于某些原因,如果您使用自定义架构名称,你其实移动__MigrationHistory表从默认dbo架构,这当然是不正确的。

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.

或者,你有没有找到一个解决方案?

Or, did you find a solution for that?

BR弗洛里安

这篇关于使用IdentityDbContext与$ C C首先自动迁移表的位置和模式$实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆