0

I can't seem to figure this one out. I tried a solution along the lines of information found here: ASP.Net Forum, but that issue is different enough to not work for me.

This is my scenario:

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);
    }

What i am trying to achieve:

  • User 1 should be able to ban user 2 With BannedBy=Contractor
  • User 2 should be able to ban user 1 With BannedBy=Customer

The exception i am currently getting is:

Cannot create a relationship between 'ApplicationUser.Bans' and 'CustomerContractorBan.Customer', because there already is a relationship between 'ApplicationUser.Bans' and 'CustomerContractorBan.Contractor'. Navigation properties can only participate in a single relationship

Am i doing this wrong or is this a limitation of efcore 2.1? I've seen similar code resolve similar issues. However in my case i can't seem to call the modelbuilder twice without getting an exception.

PS: This used to work until i added BannedBy to the properties+keys. Obviously either participant should be able to ban each other.

Dbl
  • 5,634
  • 3
  • 41
  • 66
  • Well, from my answer from the second link and the exception message it should be clear that you cannot use **single** collection navigation property for **two** relationships. Create **two** collection navigation properties and link them to the corresponding reference navigation properties (e.g. `Contractor` -> `ContractorBans`, `Customer` -> `CustomerBans`) – Ivan Stoev May 03 '18 at 08:57
  • @IvanStoev Your comment answers this question and solves the issue - please turn it into an answer so i can accept it – Dbl May 04 '18 at 00:25
  • Glad it helped :) But can't turn it to answer because it would be the exact duplicate of [Entity Framework Core: many-to-many self referencing relationship](https://stackoverflow.com/questions/49214748/entity-framework-core-many-to-many-self-referencing-relationship) – Ivan Stoev May 04 '18 at 07:19
  • 1
    @IvanStoev I see. I'll leave it as is then in case someone else gets confused too by other properties, which are part of the key but have nothing to do with the relation failing. Thanks again! Much appreciated – Dbl May 05 '18 at 19:12

0 Answers0