Override "dbo" schema name in EntityFramework Code First Migrations

c# ef-migrations entity-framework-6 oracle

Question

I'm trying to create an schema independent model with EntityFramework Codefirst and an Oracle database but EF uses as defaults for migrations dbo as schema. I overridden OnModelCreating method on my DBContext to solve this and use the user in the connectionString instead

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   base.OnModelCreating(modelBuilder);
   modelBuilder.HasDefaultSchema(string.Empty);
}

The problem is that __MigrationHistory ignores this default schema and I get this error when running first migration:

ORA-01918: User 'dbo' does not exist

Tried this msdn entry to customize the schema for this table.

CustomHistoryContext:

public class CustomHistoryContext : HistoryContext
{
    public CustomHistoryContext(DbConnection dbConnection, string defaultSchema)
            : base(dbConnection, defaultSchema) {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.HasDefaultSchema(String.Empty);
    }
}

And DBConfiguration:

public sealed class Configuration :
        DbMigrationsConfiguration<Model.MyDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        SetHistoryContextFactory("Oracle.ManagedDataAccess.Client",
                                 (connection, defaultSchema) => new CustomHistoryContext(connection, defaultSchema));
    }

    protected override void Seed(Model.Model1 context)
    {
    }
}

And is working fine for the first migration. But when I modify my entity model and try to reflect this change with add-migration command I get the following error:

Unable to generate an explicit migration because the following explicit migrations are pending: [201706281804589_initial, 201706281810218_pp2]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

Looks like EF gets lost and can't find migrations history at this point.

When I comment the SetHistoryContextFactory instruction in Configuration it works for subsequent add-migration commands but this workaround isn't enough for scenarios when I want to run all migrations from scratch like deploying.

Does anyone knows if I'm in the good way to accomplish this or if there is a better workaround for this?

1
1
6/28/2017 7:00:07 PM

Popular Answer

I successfully tried the following inside the Configuration : DbMigrationsConfiguration class for Oracle, in order to change the history schema to "Test":

var historyContextFactory = GetHistoryContextFactory("Oracle.ManagedDataAccess.Client");
SetHistoryContextFactory("Oracle.ManagedDataAccess.Client",
                         (dbc, schema) => historyContextFactory.Invoke(dbc, "Test"));

So basically, instead of trying to register a custom history context with unchanged default schema, I tried to register the default history context with changed default schema.

The result: when I run Update-Database -Script, the resulting script contains the new schema for creation of the __MigrationHistory table as well as for inserting the new history values:

create table "Test"."__MigrationHistory"
    -- ...

insert into "Test"."__MigrationHistory"("MigrationId", "ContextKey", "Model", "ProductVersion") ...

However, lets be perfectly clear: I just tried what I expected to work by intuition and it did work for me. I didn't find any reliable documentation to support this solution.

0
1/25/2019 1:40:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow