Schema independent Entity Framework Code First Migrations Schema independent Entity Framework Code First Migrations oracle oracle

Schema independent Entity Framework Code First Migrations


You can create a derived DbContext and "override" modelBuilder.HasDefaultSchema(...) in OnModelCreating:

public class TestDbContext : ProductionDbContext{    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {        base.OnModelCreating(modelBuilder);        modelBuilder.HasDefaultSchema("TestSchema");    }}

Then you can create migrations for both contexts. See this question on how to create two migrations in one project.

The downside of this approach is that you have to maintain two seperate migrations. But it gives you the opportunity to adjust the configuration of your TestDbContext.


I was faced to same problem and thanks to your aproach I finally found a solution that seems to work pretty well:

1) I have the schema name in Web.config app settings:

<add key="Schema" value="TEST" />

2) I have a history context:

public class HistoryDbContext : HistoryContext{    internal static readonly string SCHEMA;    static HistoryDbContext()    {        SCHEMA = ConfigurationManager.AppSettings["Schema"];    }    public HistoryDbContext(DbConnection dbConnection, string defaultSchema)            : base(dbConnection, defaultSchema)    { }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {        base.OnModelCreating(modelBuilder);        modelBuilder.HasDefaultSchema(SCHEMA);    }}

3) I have a db configuration that reference my history db context:

public class MyDbConfiguration : DbConfiguration{    public MyDbConfiguration()    {        SetDefaultHistoryContext((connection, defaultSchema) => new HistoryDbContext(connection, defaultSchema));    }}

4) And this is my db context:

public partial class MyDbContext : DbContext{    public MyDbContext()        : base("name=MyOracleDbContext")    { }    public static void Initialize()    {        DbConfiguration.SetConfiguration(new MyDbConfiguration());        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Migrations.Configuration>());    }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {        modelBuilder.HasDefaultSchema(string.Empty);    }}

5) Finally I call the Initialize method from the global.asax

protected void Application_Start(){    MyDbContext.Initialize();}

The key is to set the default schema of the db context to String.Empty and the schema of the history context to the correct one.So when you create your migrations they are schema independant: the DefaultSchema variable of the resx of the migration will be blank. But the history db context schema is still correct to allow migrations checks to pass.

I am using the following nugets packages:

<package id="EntityFramework" version="6.2.0" targetFramework="net452" /><package id="Oracle.ManagedDataAccess" version="12.2.1100" targetFramework="net452" /><package id="Oracle.ManagedDataAccess.EntityFramework" version="12.2.1100" targetFramework="net452" />

You can then use Oracle migrations with success on different databases.