Course contentsShow
← C#
Lesson 1042 of 1,90810. Entity Framework Core ModelingFree lesson

Owned Entity Types

Model value objects and embedded data structures using owned entities within a single table.

In this lesson, you will learn how to use Owned Entity Types to map complex objects directly into the table of their parent entity, effectively creating a "value object" pattern within Entity Framework Core.

The Problem: Flattening Your Domain

Sometimes your domain model includes objects that aren't entities themselves—they don't have an independent identity or lifecycle. For example, a User entity might have a HomeAddress property. While you want to keep your code clean by separating Address into its own class, you don't necessarily want to create a separate Addresses table in your database. This would force you to handle primary keys and joins for data that essentially just describes the user.

The Solution: Owned Entities

Owned Entity Types allow you to group related properties together in C# while keeping them stored in the same database table as the owner. EF Core tracks these owned types as part of the parent entity's state.

To define an owned type, you use the OwnsOne method within your OnModelCreating configuration:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Address HomeAddress { get; set; } // The owned entity
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
}

// In your DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
        .OwnsOne(u => u.HomeAddress);
}

By default, EF Core prefixes the database column names with the property name (e.g., HomeAddress_Street and HomeAddress_City).

When to use this?

Use Owned Entity Types when you have a set of related properties that logically belong to an entity but don't need their own unique identity. It keeps your database schema simple and avoids the performance overhead of joining multiple tables, while maintaining a clean, object-oriented structure in your domain code. It is the perfect tool for embedding value objects like addresses, coordinates, or money types directly into your aggregate root's table.

Key Takeaway: Owned Entity Types allow you to group related properties into a single C# class while EF Core transparently persists them within the owner's database table, effectively modeling value objects without the complexity of extra tables.