entity-framework-relaciones-uno-a-muchos

One to Many Relationships in Entity Framework

  • 4 min

A one-to-many (1) relationship occurs when an entity A can have many instances of an entity B, but each instance of B is related to only one A.

In other words, a record in one table can be related to several records in another table, but a record in the second table can only be associated with one record in the first table.

This is the most common type of relationship in databases.

Let’s model a system where:

  • One Blog can have many Posts.
  • Each Post belongs to only one Blog.
public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    
    // Navigation property (1 → N)
    public ICollection<Post> Posts { get; set; } = new List<Post>();
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    
    // Implicit foreign key (BlogId)
    public int BlogId { get; set; }
    
    // Navigation property (N → 1)
    public Blog Blog { get; set; }
}
Copied!

Configure the Relationship

Generated Database Structure

When applying a migration, EF creates the tables:

CREATE TABLE Blogs (
    BlogId INT PRIMARY KEY IDENTITY,
    Url NVARCHAR(MAX)
);

CREATE TABLE Posts (
    PostId INT PRIMARY KEY IDENTITY,
    Title NVARCHAR(MAX),
    Content NVARCHAR(MAX),
    BlogId INT NOT NULL,
    FOREIGN KEY (BlogId) REFERENCES Blogs(BlogId)
);
Copied!

BlogId in Posts is a non-unique foreign key (allows multiple posts per blog).

Advanced Configurations

Besides basic relationships, Entity Framework allows advanced configuration of aspects of the relationships between entities. This includes:

Relationship with Cascade Delete

When a related entity is deleted, cascade delete ensures that all related entities are also deleted.

modelBuilder.Entity<Book>()
    .HasOne(l => l.Author)
    .WithMany(a => a.Books)
    .OnDelete(DeleteBehavior.Cascade);
Copied!

Read-Only Relationship

In some situations, you may not want to allow a relationship to be modified in an entity.

modelBuilder.Entity<Book>()
    .HasOne(l => l.Author)
    .WithMany(a => a.Books)
    .OnDelete(DeleteBehavior.Restrict);
Copied!

Optional Relationships

Some relationships are optional, meaning an entity may not have a relationship with the other.

modelBuilder.Entity<Book>()
    .HasOne(l => l.Author)
    .WithMany(a => a.Books)
    .IsRequired(false);  // Optional relationship
Copied!

Practical Examples