Search code examples
c#entity-framework-coreentity-framework-migrationsentity-framework-core-migrationsentity-framework-core-2.2

Entity Framework Core compiler errors on second migration


I have a DBContext (inheriting from IdentityDbContext) holding the ASP.NET Identity tables and some custom tables.

I created a migration for it which worked fine.

Now I added some DBsets to the DbContext and called

dotnet ef migrations add SecondMigration ...

This results in 2 compiler errors after the migration was created.

Duplicate 'DbContext' attribute

and

Type 'IdDbContext' already defines a member called 'BuildModel' with the same paramter types.

I don't get it why this is because I did the exact same thing in a different project (for a different dbcontext not inheriting from identitydbcontext) two days ago and it worked without problems.

This thing is driving me already a bit crazy right now ...

Any ideas what could be the cause that the second time I get the compiler errors?


Solution

  • And finally I figured out the reason. Unfortunatly not the source of the problem but at least a fix that works manually.

    The add migrations command creates a namespace for each migrations.

    So I have ...Migrations.FirstMigration and ...Migrations.SecondMigration

    During the creation of the second migration it creates the files

    • Timestamp_SecondMigration.cs
    • Timestamp_SecondMigration.Designer.cs
    • IdDbContextModelSnapshot.cs

    The first two are created in the namespace SecondMigration but for some reason it creates the third file in the namespace FirstMigration. That causes the problem.

    Just change the namespace from FirstMigration to SecondMigration and the compiler errors are gone...

    Hopefully this does not have any negative side effects I don't know yet...


    If you have 1 DbContext with 3 migrations, should there be 1 ModelSnapshot or should there be 3 ModelSnapshots?