实体typesMVC5 EF6中的用户

我在MVC5中创build了一个类,我想要内容的主要所有者,然后我想要一些内容的编辑器:

public class Content { public int ID { get; set; } public IdentityUser Owner { get; set; } public ICollection<IdentityUser> Editors { get; set; } public string Title{ get; set; } public string Body { get; set; } } 

在数据库上下文中,我有以下代码:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Content>() .HasOptional(c => c.Editors) .WithRequired() .WillCascadeOnDelete(); modelBuilder.Entity<Content>() .HasRequired(c => c.Owner) .WithOptional() .WillCascadeOnDelete(); } 

我期望微软已经以这样的方式实现了IdentityUser对象,它可以在其他实体types中使用,所以我可能做错了什么,因为当我尝试为内容实体types创build控制器时,我得到以下错误:

 EntityType 'IdentityUserLogin' has no key defined EntityType 'IdentityUserRole' has no key defined EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no key defined EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no key defined 

我也试图添加下面的代码到ApplicationDbContext ,如在这个问题中描述的使用stream利的API在asp.net MVC5 EF6中的表? :

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); 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 }); } 

我一定做错了什么。 请告诉我如何处理Content EntityType中的用户。

下面的方法OnModelCreating将创build一个工作上下文。 不知道是否是你想要的映射。

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Content>() .HasMany(c => c.Editors) .WithOptional() .WillCascadeOnDelete(false); modelBuilder.Entity<Content>() .HasRequired(c => c.Owner) .WithOptional() .WillCascadeOnDelete(false); 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 }); } 

我有同样的问题,我发现我没有打电话

  base.OnModelCreating(modelBuilder); 

在我的DbModel