Entity Framework Core 2.0 not finding migration



I have a strange problem that my migration is not recognised by the EF Core 2.0

When running the Add-Migration command in PM, the MyFirstMigration classes are created inside the Migrations folder.


Protected Overrides Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder )
    optionsBuilder.UseSqlite("Data Source=blogging.db")
End Sub

PM code:

PM> Add-Migration MyFirstMigration 
To undo this action, use Remove-Migration.
PM> Update-Database
No migrations were applied. The database is already up to date.

When I check the dababase file, __EFMigrationsHistory table exists, but not the Blog table. As you may suspect, running db.SaveChanges() throws an exception and tells me that table doesn't exists.

Running db.Database.Migrate() does nothing, but when I delete the db file and run db.Database.EnsureCreated(), correct database is created.

I must point out that __EFMigrationsHistory table is created empty, so I can immediately after Update-Database create the next migration and it will generate exactly the same code as in the first one.

I am using VS 15.3.5 and .Net 4.6.1 on WPF.

/ Best regards

10/9/2017 12:59:42 PM

Accepted Answer

Is it because Add-Migration is adding C# files to your VB.NET project? You can add them to a separate C# project, reference it from your DbContext assembly, and add update your OnConfiguring to the following.

optionsBuilder.UseSqlite("Data Source=blogging.db", Sub(x) x.MigrationsAssembly("MyCSharpProject"))
10/9/2017 5:11:17 PM

Popular Answer

I had this issue where my migrations didn't contain any entities, then I noticed something that is not very obvious. The DbContext class looks at it's own member variables and then applies any fluent/attribute model properties to the generated migration.

So make sure your DbContext has something like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)

public virtual DbSet<Blog> Blogs { get; set; }

Also remember after making any changes that you need to generate a new migration. Which seems to work a bit different than before where update-database (PMC) used to always apply new changes.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow