I have two Entities in an MVC app:
public class User
{
public int ID {get; set;}
public string Forename {get; set;}
public string Surname {get;set;}
}
public class SubGroup
{
public int ID {get;set;}
public string Name {get;set;}
}
and I want to use Code First to create a relationship between them. The relationship is that each user can be a member of 0 or more subgroups.
Please can someone suggest the best way to do this because I have seen a few examples on SO and I’m confused. I’ve seen stuff about implementing ICollections in a class, other stuff about using the fluent API.
ICollections approach:
UPDATE:
If I understand correctly you have many-to-many relationship (group have many users, user have can be in many groups), in that case code should be like this:
Adding Users to groups example: