Entity Framework Core Intermediate

Relationships

Real data is connected: customers have orders, orders have lines, students enrol in courses. This lesson shows how EF Core models one-to-many, one-to-one and many-to-many relationships with navigation properties and foreign keys.

Relationship types in EF Core
  1. One-to-manyA Customer has many Orders; each Order has one CustomerId
  2. One-to-oneA User has one Profile; Profile holds the UserId
  3. Many-to-manyA Student takes many Courses and a Course has many Students; EF Core creates the join table

Navigation properties let you move between related objects in C#; foreign keys store the link in the database.

The idea in plain words

A navigation property is a property that points to another entity, such as Order.Customer, or to a collection, such as Customer.Orders. A foreign key property, such as Order.CustomerId, holds the id stored in the database. When you name things in the usual way, EF Core discovers relationships automatically by convention. When it cannot, or you want different behaviour, you configure them with the Fluent API in OnModelCreating.

Example: one-to-many and many-to-many

For many-to-many, collections on both sides are enough; EF Core creates the join table for you.

C#
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public List<Order> Orders { get; } = new();        // one customer, many orders
}

public class Order
{
    public int Id { get; set; }
    public int CustomerId { get; set; }                 // foreign key
    public Customer Customer { get; set; } = null!;     // navigation back
}

public class Student
{
    public int Id { get; set; }
    public List<Course> Courses { get; } = new();       // many-to-many
}

public class Course
{
    public int Id { get; set; }
    public List<Student> Students { get; } = new();
}

Example: configuring with the Fluent API

Use the Fluent API when conventions are not enough, for example to choose what happens to orders when a customer is deleted.

C#
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>()
        .HasMany(c => c.Orders)
        .WithOne(o => o.Customer)
        .HasForeignKey(o => o.CustomerId)
        .OnDelete(DeleteBehavior.Restrict); // block deleting customers with orders
}

Common mistakes

  • Expecting navigation properties to be filled automatically when querying; related data must be loaded with Include or a projection.
  • Leaving cascade delete on for important data by accident.
  • Serializing entities with cycles (Customer to Orders to Customer) straight to JSON; use view models or DTOs.

Interview question

How does EF Core decide that Order.CustomerId is a foreign key?

By convention: when an entity has a navigation property to another entity, EF Core looks for a property named after the navigation or principal entity plus its key, such as CustomerId or CustomerID, and treats it as the foreign key. If none is found it creates a shadow foreign key. You can override this with the [ForeignKey] attribute or the Fluent API HasForeignKey.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.