如何在 EF Core 5 中创建三向关系

How to create a 3 ways relationship in EF core 5

我正在使用 EntityFramework 5,并且我有一个模型,其中我有一个多对多对多的关系。这是我的模型:

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Email { get; set; }
    public UserStatus Status { get; set; } // Manager or user
    
    public ICollection<Contract> Contracts { get; set; }
}


public class Enterprise
{
    public int EnterpriseId{ get; set; }
    public string SIRET { get; set; }
    public string AdministrativeName { get; set; }
    public string PublicName { get; set; }
    public string Adress { get; set; }
    public string ZipCode { get; set; }
    public string City { get; set; }
    public string ContactEmail { set; get; }
    public string ContactName { get; set; }
    public string ContactPhoneNumber { get; set; }

    public ICollection<Contract> Contracts { get; set; }
}


public class Contract
{
    public int ContractId { get; set; }
    public int ConsultantId { get; set; }
    public User Consultant { get; set; }
    public int ManagerId { get; set; }
    public User Manager { get; set; }
    public int CustomerId { get; set; }
    public Enterprise Customer { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

protected override void OnModelCreating( ModelBuilder modelBuilder )
{
       modelBuilder.Entity<Contract>()
           .HasOne( c => c.Consultant )
           .WithMany( u => u.Contracts );

       /*modelBuilder.Entity<Contract>()
            .HasOne( c => c.Manager )
            .WithMany( m => m.Contracts );*/

       modelBuilder.Entity<Contract>()
            .HasOne( c => c.Customer )
            .WithMany( e => e.Contracts );

}

我有一名员工与客户签订合同。每份合同都由一名经理监督。 由于用户可以是经理或员工,我们有 2 link 向用户 table,但一个是向员工,一个是向经理。

到目前为止,它似乎并不太复杂,“添加迁移”脚本 运行 很好。但是,当我尝试 运行 UpdateDatabase 时,我得到了这个输出:

Introducing FOREIGN KEY constraint 'FK_Contract_User_ManagerId' on table 'Contract' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.Could not create constraint or index. See previous errors.

我尝试使用错误提示修复它,但没有效果。 有没有办法在 EF Core 中 link 3 个实体,或者我必须使用不同的模型?

谢谢

[编辑] 我更新了我发布的代码

假设每个合同都有一个经理和一个顾问,为用户添加导航属性

public class User
{
    public int UserId { get; set; }
    .......

    public UserStatus Status { get; set; } // Manager or user

    public ICollection<Contract> UserContracts { get; set; }
    public ICollection<Contract> ManagerContracts { get; set; }
}

如果一份合同可以有 1 名经理,但有几名顾问,或者也有几名经理,您将需要多一名 table 合同顾问。在这种情况下,您的用户将是这样的

public class User
{
    public int UserId { get; set; }
    .......

    public UserStatus Status { get; set; } // Manager or user

    public ICollection<ConsultanContract> UserContracts { get; set; }
    public ICollection<Contract> ManagerContracts { get; set; }
}

并且您必须从合同中删除

 public int ConsultantId { get; set; }
    public User Consultant { get; set; }

可行,但我必须在 OnModelCreating

中添加一些配置
protected override void OnModelCreating( ModelBuilder modelBuilder )
{
    modelBuilder.Entity<Contract>()
        .HasOne( c => c.Consultant )
        .WithMany( u => u.DevelopperContracts )
        .OnDelete(DeleteBehavior.NoAction);

    modelBuilder.Entity<Contract>()
        .HasOne( c => c.Manager )
        .WithMany( m => m.ManagerContracts )
        .OnDelete( DeleteBehavior.NoAction );

    modelBuilder.Entity<Contract>()
        .HasOne( c => c.Customer )
        .WithMany( e => e.Contracts )
        .OnDelete( DeleteBehavior.NoAction );
}