Entity Framework: One to Many relationships

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.

1tomany

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s