Entity Framework One To Many Relationship
08 August 2018
Hello everybody,
today I want to make short post on how to confiugre one to many relationship in Entity Framework 6.
Imagine following: One group can have multiple students. So one to many relationship. For this purpose you can use following convention configuration agreement:
public class Student { public int StudentId { get; set; } public string StudentName { get; set; } } public class Group { public int GroupId { get; set; } public string GroupName { get; set; } public string Department { get; set; } public List<Student> Students { get; set; } }
In presented example class group includes navigation property Students.
Another convention is like this:
public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public Group CurrentGroup { get; set; } } public class Group { public int GroupId { get; set; } public string GroupName { get; set; } public string Department { get; set; } public List<Student> Students { get; set; } }
as you can see, difference is that Student instance by itself knows to which Group it belongs.