2

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()?

Kyle
  • 32,731
  • 39
  • 134
  • 184

1 Answers1

2

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

base.OnModelCreating(modelBuilder); 

Credit to @Augusto Barreto in comments.

Kyle
  • 32,731
  • 39
  • 134
  • 184
  • I was hopeful, because - using MVC5 - I had the exact same error as @Kyle after doing the same fixes (3x). But for me adding the call to `base` I still got the exact same error :S. – Bart Nov 19 '15 at 01:38