I recently migrated our .NET 1.1.4 code to .NET 2.0.0 Everything seems to work fine. Now, I tried adding a new model/table by doing:
dotnet ef update
I get the following exception:
DbInitializer failed! A key cannot be configured on 'ApplicationUser' because it is a derived type. The key must be configured on the root type 'IdentityUser'
I have an ApplicationUser that extends the IdentityUser and ApplicationRole that extends the IdentityRole, which is what some of the other tutorials/ stackoverflow posts say to do.
I'm not sure what else I could be missing.
In my ApplicationUser class I have:
ICollection<IdentityUserRole<string>>
Does that have to be of a custom type like ApplicationUserRole? What's the difference between IdentityUserRole and IdentityRole?
EDIT:
I think it may be because in my context.cs file in OnModelCreating I have:
builder.Entity<ApplicationUser>()
.ToTable("AspNetUsers")
.HasKey(x => x.Id);
Here Id is inherited from IdentityUser.
EDIT2:
So I commented out that line and that error goes away. It goes to the next line which is basically the same thing but for my ApplicationRole which extends IdentityRole. I'm not quite sure what this means. Do i need to override the id property?
EDIT3:
OK so I changed the types in the builder.Entity to just be IdentityUser and IdentityRole. Why does that fix it when in startup.cs I have to use ApplicationUser and ApplicationRole when calling services.AddIdentity<ApplicationUser, ApplicationRole>
It sounds like somewhere in your project you are referencing IdentityUser.
Ctrl + F "IdentityUser" entire solution then change any and all references to IdentityUser to ApplicationUser (other than the where you derive from it in your ApplicationUser class).
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With