User in Entity type MVC5 EF6

后端 未结 2 614
南方客
南方客 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()
            .HasMany(c => c.Editors)
            .WithOptional()
            .WillCascadeOnDelete(false);
    
        modelBuilder.Entity()
            .HasRequired(c => c.Owner)
            .WithOptional()
            .WillCascadeOnDelete(false);
    
        modelBuilder.Entity().HasKey(l => l.UserId);
        modelBuilder.Entity().HasKey(r => r.Id);
        modelBuilder.Entity().HasKey(r => new { r.RoleId, r.UserId });
    }
    

提交回复
热议问题