Search code examples
entity-framework-coreandroid-sqliteentity-framework-migrations

EntityFramewor.Core does not migrate sqite database


I have a code first model, e.g.:

 public class Cars
 {
     public int Id { get; set; }            
     public string Brand { get; set; }        
 }     

My DBContext looks like this:

public class TestDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Data Source = data.db");
    }

    DbSet<Car> Cars { get; set; }
}

Now when I call "context.Database.Migrate" a _EFMigrationHistory table gets created but no migrations are happening. No tables or columns get created.

And when I call "update-database" in the command line it says "No migrations were applied. The database is already up to date." although there is a "Cars" table to add

What am I doing wrong?


Solution

  • It appears you might have forgotten to create your first migration. Every time you change your object model in a way that needs applied to the database, you'll need to create a migration - this is not automatic so you can customize the migration code if needed and batch multiple changes into one logical group.

    In the package manager console (command line), set your default project to whatever project houses your TestDbContext, and run the add-migration command with a name for the migration, then your project will have a migration to run:

    PM> add-migration MyInitalMigration
    

    Package Manager Console Example Image