User in Entity type MVC5 EF6

后端 未结 2 580
南方客
南方客 2020-11-27 03:49

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
{
             


        
相关标签:
2条回答
  • 2020-11-27 04:27

    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 });
    }
    
    0 讨论(0)
  • 2020-11-27 04:34

    I had the same problem and I found that I had not called

     base.OnModelCreating(modelBuilder);
    

    in my DbModel

    0 讨论(0)
提交回复
热议问题