Skip to content

Latest commit

 

History

History
80 lines (57 loc) · 3.64 KB

2. Entity relationships.md

File metadata and controls

80 lines (57 loc) · 3.64 KB

Entity relationships

Our entity classes are usually related in one way or another. The most common relationships are:

  • One-to-one. In this case, we've got exactly one instance of entity A mapping to exactly one instance of entity B, and vice versa. Example: a car has exactly one steering wheel, and a steering wheel is in exactly one car.
  • One-to-many. In this case, one instance of entity A maps to 0 or more instances of entity B. A single instance of entity B then maps to a single instance of entity A. Example: A course is taught on a given semester. The given semester has therefore many courses, but each course instance only belongs to a given semester.
  • Many-to-many. This is the most flexible option: a given instance of entity A is related to 0 or more instances of entity B, and each entity B may be related to 0 or more of entity A. Example: a course instance may have many teachers, and each teacher may be teaching many courses.

In databases, when entity A is related to a single entity B (which can happen in one-to-one, and in one-to-many), this usually means that there is a foreign key in the table which stores B, and that foreign key references A.

In the case of a many-to-many relationship, the design usually requires us to create an intermediate table which stores information about the relationship. Each entry in that table will then contain information about a single relationship, i.e. the fact that a single instance of A is related to a single instance of B.

Entity Framework relationships

The Entity Framework supports relationships in various ways. Specifying one to one relationship can be done in entity class A using the C# keyword virtual for members of entity class type B:

public class Car
{
    public int ID { get; set; }
    public string Name { get; set; }
    
    // A reference to the single steering wheel the car has:
    public virtual SteeringWheel SteeringWheel { get; set; }
    
    // Note that the reference is usually implemented using a foreign key.
    // We can explicitly specify that as well:
    public int SteeringWheelID { get; set; }
}

public class SteeringWheel 
{
  // etc.
}

A one-to-many relationship would be specified like this:

public class Semester
{
    public string ID { get; set; } // Example: "20143"
    public string Name { get; set; } // Example: "Fall 2014"
    
    // A collection of all the courses taught on this semester:
    public virtual ICollection<CourseInstance> Courses { get; set; }
}

public class CourseInstance
{
  public int ID { get; set; }
  
  public string SemesterID { get; set; } // This is the foreign key to the semester table
  
  [ForeignKey("SemesterID")]
  public virtual Semester Semester { get; set; }
}

Loading options

As you may have already guessed, if all related entities would be automatically loaded when a given entity is loaded, we could be in a lot of trouble. Fortunately we can fine tune exactly when entities are loaded, using the following options:

  • Lazy loading. Related entities are loaded only when they are requested.
  • Eager loading. Related entities are loaded at the same time as the main entity.
  • Explicit loading. We state explicitly how we want loading to occur.

Do note that lazy loading could result in multiple queries for the database, when a single query (or possibly two queries) would suffice.

It is therefore necessary to be aware of what is actually happening under the hood when using related entites in Entity Framework.