3

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.

Context:

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.
Done.

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dimka
  • 425
  • 4
  • 11

2 Answers2

3

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"))
bricelam
  • 28,825
  • 9
  • 92
  • 117
  • 1
    What a strange thing to do. I can also see that it adds Microsoft.CSharp reference and that is probably why VS doesn't complains about cs files. Basically that leaves VB anf F# (believe they have the same problem) people without a real ORM because I have hard to believe that EF6 is much easier to use. That kind of things should really be in the docs as 'limited support for'. – Dimka Oct 10 '17 at 06:25
  • 2
    See also issues [#6401](https://github.com/aspnet/EntityFrameworkCore/issues/6401) and [#8012](https://github.com/aspnet/EntityFrameworkCore/issues/8012). – bricelam Oct 10 '17 at 18:42
  • @bricelam not so much related, how can I call `context.Database.Migrate` post `OnConfiguring`? I have a dynamic per-tenant configuration and I want the DB to be generated once the tenant info is available. Posted [here](https://stackoverflow.com/questions/29110241/how-do-you-configure-the-dbcontext-when-creating-migrations-in-entity-framework/40852034#comment99320410_40852034) as well. – Shimmy Weitzhandler May 29 '19 at 10:30
0

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.

John
  • 3,512
  • 2
  • 36
  • 53