Answer the question
In order to leave comments, you need to log in
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; }
}
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; }
}
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
modelBuilder.Entity<Ticket>()
.HasOne(t => t.Customer)
.WithMany(u => u.AsCustomerTickets);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question