...

/

Relationships in Entity Framework Core

Relationships in Entity Framework Core

Learn about the different types of entity relationships in EF Core.

Overview

In EF Core, relationships define how two entities relate and how the database reflects the relationship. When dealing with relational databases, EF Core uses foreign keys to establish the relationships between entities. This lesson introduces the concepts surrounding the relationship between entities.

The code sample below illustrates some concepts regarding relationships in EF Core:

Press + to interact
Employee.cs
Album.cs
namespace Relationships
{
public class Employee
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public long Age { get; set; }
public ICollection<Album> Albums { get; set; }
}
}

The code sample illustrates the relationship between the Employee and Album entities. It highlights the following concepts:

  • Dependent entity: This is the entity that contains the foreign key properties. It is also known as the child of the relationship. The Album entity is the dependent entity in the code sample. Line 9 of Album.cs highlights a foreign key, and line 10 contains a reference navigation property to the Employee entity.

  • Principal entity: This ...