Reputation: 1588
I have this classes:
public class User
{
[Key]
public Guid UserId { get; set; }
public string Address { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string Units { get; set; }
public int Distance { get; set; }
public List<Sport> Sports { get; set; }
}
public class Sport
{
[Key]
public int SportId { get; set; }
public string Name { get; set; }
public List<User> Users { get; set; }
}
So, these classes, with the foreign keys many to many, generates in the database a table like that: SportUsers (SportId, UserId)
When a User, sign up, select from a listbox of Sports, the Sports in which he is interested. So in the Register action in the appropiate controller, I save the user like that:
db.Users.Add(user);
db.SaveChanges();
But, how can I save the list of sports in which the user is interested, in the autogenerated for the foreign key, SportUsers table ???
Upvotes: 1
Views: 903
Reputation: 12476
If your User has a collection of Sports, Entity Framework will figure out that that's a M*N relationship and automatically fill the relationship in your database.
Upvotes: 1