Entity Framework using IdentityDbContext with layout and layout of First First Migrations?

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

Before I get into the code, when implementing IdentityDbContext with automatic transitions, I get this error:

Automatic migrations that affect the location of the system migration log table (for example, changing the default schema) are not supported. Please use code-based migration for operations that affect the location of the system migration history table.

I will not publish models related to migrations and context code unless someone finds them to use.

Implementation of 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 to add this class to put the migration history in the correct schema. This actually translates the migration 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 would like everything to be in ft . I do not think that migration is so complicated that I need to manually configure the migration. I was hoping for simplicity, I could use automatic migrations for this. I am wondering if this approach is impossible to do and what I need to do to make this happen, and any changes made to the models are propagating.

+7
c # entity-framework entity-framework-6
source share
1 answer

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

 modelBuilder.HasDefaultSchema("") 

in my DbContext, to get the runtime, see the tables in the specific user's login scheme. For the update database, you had to set the MyHistoryContext parameters as follows:

 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. Thus, the update database does not try to use dbo as a schema (but automatic migrations are not possible, they will drop your MigrationHistory table and ruin everything). This, in my opinion, is a nasty bug inside EF6 or the regular Oracle class. Since I do not have a service contract with them, I cannot apply for a ticket.

In your case, I think it cannot be developed in any way to avoid an error message with automatic transitions. For some reason, EF6 thinks that if you use your own schema name, you are actually moving the __MigrationHistory table from the default dbo schema, which of course is incorrect.

Or did you find a solution to this?

BR Florian

+2
source share

All Articles