2
votes

I'm using MVC5, with the default template and identity 2.0 user system with EF6.

I added OnModelCreating, to my ApplicationDbContext : IdentityDbContext<ApplicationUser> class, in order to use the fluid API to create a many-many relationship between two classes.

This caused these errors to come up when I ran update-database, even though I did not make any chages to these tables:

EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

Searching the internet, I found on another stackoverflow question to add this to my OnModel Creating:

      modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

This solved those errors, but now I have this error:

Cannot find the object "dbo.AspNetUserClaims" because it does not exist or you do not have permissions.

How do I fix this last error, and why do I have all of these errors to fix once I introduce OnModelCreating()?

1
I think the best solution is just to call base.OnModelCreating(modelBuilder); from your OnModelCreatingAugusto Barreto

1 Answers

2
votes

Calling the base class OnModelCreating, at the end of my overridden one, fixes the problem:

base.OnModelCreating(modelBuilder); 

Credit to @Augusto Barreto in comments.