I have created a class in MVC5, where I want a primary owner of the content and then I want to have some editors for the content:
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; } }
In the database context I have the following code:
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(); }
I would expect Microsoft to have implemented the IdentityUser
object in such a way, that it can be used in other entity types, so I am probably doing something wrong, because when I try to make a controller for the Content entity type, I get the following error:
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
I have also tried to add the following code to the ApplicationDbContext
as described in this question Map tables using fluent api in 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 }); }
I must be doing something wrong. Please tell me how I can handle users in my Content EntityType.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I had the same problem and I found that I had not called
base.OnModelCreating(modelBuilder);
in my DbModel
Method 2
The following method, OnModelCreating
, will create a working context. Not sure if it is the mapping you want though.
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 }); }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0