MVC5 Identity EF 6 Fluid API 错误

MVC5 Identity EF 6 Fluid API errors

我使用的是 MVC5,默认模板和身份 2.0 用户系统与 EF6。

我将 OnModelCreating 添加到我的 ApplicationDbContext : IdentityDbContext<ApplicationUser> class,以便使用流体 API 在两个 class 之间创建多对多关系]es.

这导致在我 运行 update-database 时出现这些错误,即使我没有对这些表进行任何更改:

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.

在互联网上搜索,我在另一个 Whosebug 问题上找到了将其添加到我的 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 });

这解决了那些错误,但现在我遇到了这个错误:

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

如何修复最后一个错误,为什么在我引入 OnModelCreating() 后要修复所有这些错误?

调用基础 class OnModelCreating,在我重写的最后,修复了问题:

base.OnModelCreating(modelBuilder); 

在评论中感谢@Augusto Barreto。