Override "dbo" schema name in EntityFramework Code First Migrations

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?


Try runnig Enable-Migrations -force again. Then run Add-Migration SomeDescription –IgnoreChanges . After that, run " Update-Database - Verbose ". This worked to me

链接地址: http://www.djcxy.com/p/15986.html

上一篇: Android WebBrowser向下滚动页面

下一篇: 在EntityFramework Code First Migrations中覆盖“dbo”模式名称