D
D
D8i2019-11-06 18:01:39
ASP.NET
D8i, 2019-11-06 18:01:39

How to implement multiple many-to-many in one entity?

And so, I have two entities:
Ticket:

public class Ticket
{
    public long Id { get; set; }

    public long CustomerId { get; set; }
    public User Customer { get; set; }
        
    public ICollection<User> Partners { get; set; }
    public ICollection<User> Maintainers { get; set; }
}

and user:
public class User
{
    public long Id { get; set; }

    public ICollection<Ticket> AsCustomerTickets { get; set; }
    public ICollection<Ticket> AsPartnerTickets { get; set; }
    public ICollection<Ticket> AsMaintainerTickets { get; set; }
}

The links I need:
1. User Customer -> one-to-many -> ICollection<Ticket> AsCustomerTickets
2. ICollection<User> Partners -> many-to-many -> ICollection<Ticket> AsPartnerTickets
3. ICollection<User> Maintainers -> many-to-many -> ICollection<Ticket> AsMaintainerTickets

First one (one-to-many) implemented with modelBuilder:
modelBuilder.Entity<Ticket>()
                .HasOne(t => t.Customer)
                .WithMany(u => u.AsCustomerTickets);

The rest just don't work for me. Entity Framework does not yet support many-to-many, an alternative was proposed in the EF documentation, through the creation of a certain TicketUser (in my case) - but as I understand it, it is suitable for one many-to-many bundle. Please tell me what can I do in my situation...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HRAshton, 2019-11-20
@D8i

The MM relationship is a violation of the third normal form of the database and is not supported by the very concept of relational DBMS. To implement such a relationship in a relational database, you need to make a third table - like Entity1_Entity2_Relationship - and implement One-to-Many relationships:
Entity1 >--- Entity1_Entity2_Relationship --< Entity2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question