A one-to-many relationship in CodeFirst is exactly like a zero-or-one to many relationship; you only need to add one line of code to the “many” side of the relationship. The highlighted field is the only line you have to add, everything else is the same.
namespace g20161005_ChurchData { public class Person { public Person() { } public int id { get; set; } public int parishID { get; set; } public string fname { get; set; } public string lname { get; set; } } public class Parish { public Parish() { this.Persons = new HashSet<Person>;(); } public int id { get; set; } public int PersonID { get; set; } public virtual ICollection<Person> Persons { get; set; } } }
Advertisements