EntityFrameworkCore for Sqlite not creating __EFMigrationHistory table

entity-framework-core sqlite uwp

Question

Before I explain my issue, I have some experience with entity framework 5 and 6 code first migrations, running add-migration/update-database and a few more specific commands from the Package Manager console. All of the migration history was handled out of the box in the __MigrationHistory table.

I am now writing a UWP app and using EntityFrameworkCore sqlite. The app is set up to scaffold new migrations and does so correctly.

When applying migrations the app needs to automatically deduce, on install and first startup, if the database exists, and the current database migration version. It can then apply the relevant migration procedures, including creating the database if required.

Currently, I attempt to perform the migrations in my DbContext on startup:

public class MyContext : DbContext
{        
    public DbSet<SomeEntity> MyEntities { get; set; }

    static MyContext()
    {
        using(var db = new MyContext())
        {
            db.Database.Migrate();
        }
    }

This works perfectly for a new app on first startup. On second startup however, or after the addition of a new migration, the Migrate() method fails as the tables it is attempting to create already exist.

SQLite Error 1: 'table \"MyEntities\" already exists'

This error comes from rerunning the migration that has been previously applied. The database itself needs to be aware of it's migration history as was previously handled with __EFMigrationHistory. Currently this table is not being created for me.

I am suspecting that I need to manually build a solution to this, maybe creating my own __MigrationHistory table and keeping it up to date, as per this post here

I wondered what solutions people have used for this issue, or if there is anything out of the box that I'm being silly and missing.

Let me know if more detail needed.

1
1
2/22/2017 3:13:19 PM

Popular Answer

As far as i've come up with while having your same issue, i found out the debug database (inside your \bin\Debug folder) won't have the __EFMigrationsHistory table, while the production database (root of your launching project) has it.

Maybe could be of help for somebody else.

0
2/13/2019 1:20:13 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