Entity Framework code first auto migration not working with Oracle database

Viewed 277

I have some issue with an Oracle database. When I run my Windows Forms application using Entity Framework 6 to apply all database related changes to the Oracle database, I get this error:

Automatic migrations that affect the location of the migrations history system table (such as default schema changes) are not supported.
Please use code-based migrations for operations that affect the location of the migrations history system table.

Oracle database version: "Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production Version 18.4.0.0.0"

I am using a code-first approach with auto migration enabled. This code first approach working perfectly when I connect to SQL Server database (note: SQL Server database connection only for cross check) but have this issue with Oracle.

What I tried from my side

I added code-based migration script i.e. Add-Migration CreateNewDB and then applied this migration to Oracle database and it works.

But I want to auto-update the database and apply any changes to the Oracle database which is still not working automatically (auto migration). Currently I need to create a code-based migration and apply that to Oracle database every time.

Sample code on model create

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);
            
            modelBuilder.HasDefaultSchema("PDBADMIN");
       
       
            modelBuilder.Entity<ADHOCCHECK>()
                .Property(e => e.sortrev)
                .IsUnicode(false);

            modelBuilder.Entity<ADHOCCONSTRAINT>()
                .Property(e => e.fldtype)
                .IsUnicode(false);

            modelBuilder.Entity<ADHOCCONSTRAINT>()
                .Property(e => e.fldstr1)
                .IsUnicode(false);
                }

Any help would be appreciated.

Thanks!

1 Answers

You are changing your schema to PDBADMIN in this line modelBuilder.HasDefaultSchema("PDBADMIN");. Unfortunately, you can't use automatic migrations with the Oracle provider using a custom schema name.

From Oracle's documentation

Code First Automatic Migrations is limited to working with the dbo schema only. Due to this limitation it is recommended to use code-based migrations, that is, add explicit migrations through the Add-Migration command.

So you have to either use the default schema name dbo, or disable automatic migrations and use code based migrations.

In my personal opinion, even if you hadn't come across this particular problem with the Oracle provider, I would strongly recommend usindg code based migrations anyway.

It's also the official recommendation when working on a team:

You can intersperse automatic and code-based migrations but this is not recommended in team development scenarios. If you are part of a team of developers that use source control you should either use purely automatic migrations or purely code-based migrations. Given the limitations of automatic migrations we recommend using code-based migrations in team environments.

Related