Override "dbo" schema name in EntityFramework Code First Migrations

c# ef-migrations entity-framework-6 oracle

Question

With EntityFramework Codefirst and an Oracle database, I'm attempting to develop a schema-independent model, however EF utilises dbo as the default schema for migrations. To fix this, I overrode the OnModelCreating function on my DBContext and substituted the user in the connectionString.

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

This default schema is ignored by __MigrationHistory, which is why I get the following error while conducting the initial migration:

ORA-01918: User 'dbo' does not exist

I tried this msdn entry to alter this table's structure.

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

DBConfiguration is also:

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 the first move went well. However, when I alter my entity model and attempt to reflect this modification usingadd-migration command I encounter the following issue:

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.

It seems that EF got lost and is now unable to locate the history of migrations.

When I make a remark,SetHistoryContextFactory education inConfiguration It is effective for upcomingadd-migration scripts, but in situations when I want to conduct all migrations from scratch, such as deploying, this workaround is insufficient.

Anyone know whether I'm going about this the right way or if there is a better way to go past this?

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

Popular Answer

ZZZ_tmp
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