I have these two classes set up and I want Entity Framework to automatically create the resulting many-to-many relationship tables for set of entities.
public class User {
public int Id { get; set; }
public ICollection<Event> SignedUpEvents { get; set; }
public ICollection<Event> CompletedEvents { get; set; }
}
and
public class Event {
public int Id { get; set; }
public ICollection<User> SignedUpUsers { get; set; }
public ICollection<User> CompletedUsers { get; set; }
}
This does nothing. My guess is that since there are two relationships specified and both have the same entity-types involved EF can’t distinguish the two.
Is there a way to make this happen? Maybe some DataAnnotation to name the resulting tables or something?
Yeah the issue is as you say that it has two collections of ‘Event’s.
You can set up dual navigation properties using DataAnnotations however my personal preference is to use the model builder. This keeps your poco classes clean and avoids unnecessary references.
I wrote a tutorial on how to use the model builder and how EF navigation properties work which i recommend reading as it goes int much more depth.
From a simple point of view you want something like the following: