Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 具有多个关键参数的多对多自引用_Entity Framework_Ef Core 2.0 - Fatal编程技术网

Entity framework 具有多个关键参数的多对多自引用

Entity framework 具有多个关键参数的多对多自引用,entity-framework,ef-core-2.0,Entity Framework,Ef Core 2.0,我似乎无法理解这一点。我尝试了一个解决方案,内容如下:,但这个问题不同,对我来说不起作用 这也不起作用:(HasMany+WithMany在Efcore中不存在) 这也不是: 这也不是: 这是我的设想: public class ApplicationUser { public int Id { get; set; } public ICollection<CustomerContractorBan> Bans { get; set; } } public e

我似乎无法理解这一点。我尝试了一个解决方案,内容如下:,但这个问题不同,对我来说不起作用

  • 这也不起作用:(HasMany+WithMany在Efcore中不存在)

  • 这也不是:

  • 这也不是:

这是我的设想:

public class ApplicationUser
{
    public int Id { get; set; }
    public ICollection<CustomerContractorBan> Bans { get; set; }
}


public enum BanType
{
    Contractor,
    Customer
}

public class CustomerContractorBan
{
    public BanType BannedBy { get; set; }

    [ForeignKey(nameof(CustomerId))]
    public ApplicationUser Customer { get; set; }

    public int CustomerId { get; set; }

    [ForeignKey(nameof(ContractorId))]
    public ApplicationUser Contractor { get; set; }

    public int ContractorId { get; set; }

    public DateTimeOffset Time { get; set; }
}


    /// <inheritdoc />
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CustomerContractorBan>().HasKey(d => new { d.ContractorId, d.CustomerId, d.BannedBy });

        modelBuilder.Entity<CustomerContractorBan>()
            .HasOne(d => d.Contractor)
            .WithMany(d => d.Bans)
            .HasForeignKey(d => new { d.ContractorId })
            .OnDelete(DeleteBehavior.Restrict);

        modelBuilder.Entity<CustomerContractorBan>()
            .HasOne(d => d.Customer)
            .WithMany(d => d.Bans)
            .HasForeignKey(d => new { d.CustomerId })
            .OnDelete(DeleteBehavior.Restrict);
    }
公共类应用程序用户
{
公共int Id{get;set;}
公共ICollection Bans{get;set;}
}
公共枚举类型
{
承包商,
顾客
}
公共类CustomerContractorBan
{
公共BanType BannedBy{get;set;}
[外键(姓名(客户ID))]
公共应用程序用户客户{get;set;}
public int CustomerId{get;set;}
[外方(合同方名称))]
公共应用程序用户承包商{get;set;}
public int ContractorId{get;set;}
公共DateTimeOffset时间{get;set;}
}
/// 
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity().HasKey(d=>new{d.ContractorId,d.CustomerId,d.BannedBy});
modelBuilder.Entity()
.HasOne(d=>d.承包商)
.有许多(d=>d.BAN)
.HasForeignKey(d=>new{d.ContractorId})
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity()
.HasOne(d=>d.Customer)
.有许多(d=>d.BAN)
.HasForeignKey(d=>new{d.CustomerId})
.OnDelete(DeleteBehavior.Restrict);
}
我正在努力实现的目标:

  • 用户1应该能够通过BannedBy=承包商禁止用户2
  • 用户2应该能够禁止BannedBy=Customer的用户1
我目前遇到的例外情况是:

无法在“ApplicationUser.Bans”和之间创建关系 “CustomerContractorBan.Customer”,因为已经存在 “ApplicationUser.Bans”和 “CustomerContractorBan.Contractor”。导航属性只能 参与单一关系

我是做错了还是这是efcore 2.1的一个限制?我见过类似的代码解决类似的问题。然而,在我的例子中,我似乎不能两次调用modelbuilder而不得到一个异常


PS:在我将BannedBy添加到属性+键之前,这一直有效。显然,任何一个参与者都应该能够相互禁止。

好吧,从我在第二个链接中的回答和异常消息中可以清楚地看到,不能对两个关系使用单一集合导航属性。创建两个集合导航属性,并将它们链接到相应的参考导航属性(例如,
承包商
->
承包商方
客户
->
客户方
)@IvanStoev您的评论回答了这个问题并解决了这个问题-请将其转换为一个答案,这样我就可以接受它(很高兴它有帮助:)但不能将其转换为答案,因为它与我看到的@IvanStoev完全相同。我将保持原样,以防其他人也被其他属性弄糊涂,这些属性是键的一部分,但与关系失败无关。再次感谢!非常感谢