Entity Framework Core Advanced

Owned and complex types

Some objects have no identity of their own, such as an address or an amount with a currency. This lesson shows how to model them as owned types and complex types so they are stored inside their parent's table while your C# stays clean.

A value object inside its parent
  1. StartCustomer entityHas an Id and a ShippingAddress
  2. Step 2Address recordStreet, City, PostalCode; no Id
  3. Step 3ComplexPropertyMaps the record to columns
  4. EndCustomers tableShippingAddress_Street, ShippingAddress_City ...

Value objects

An Address has no Id: two orders with the same street and city simply have equal addresses. In domain-driven design this is a value object. Giving it its own table with a key would be extra work for no benefit.

EF Core offers two ways to map such classes into the parent's table: owned types (since EF Core 2) and complex types (EF Core 8 and later).

Complex types (EF Core 8+)

A complex type is stored as columns of the parent table and has value semantics: it can be a record and can be shared between entities. This is the recommended choice for value objects.

C#
public record Address(string Street, string City, string PostalCode);

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public required Address ShippingAddress { get; set; }
    public required Address BillingAddress { get; set; }
}

modelBuilder.Entity<Customer>(b =>
{
    b.ComplexProperty(c => c.ShippingAddress);
    b.ComplexProperty(c => c.BillingAddress);
});
// Columns: ShippingAddress_Street, ShippingAddress_City, ... in Customers

Owned types

Owned types are tracked like entities with a hidden key. They support things complex types lacked at first: optional (nullable) values, collections with OwnsMany, and mapping to JSON.

C#
modelBuilder.Entity<Order>(b =>
{
    b.OwnsOne(o => o.Delivery, d =>
    {
        d.Property(x => x.City).HasMaxLength(100);
    });
    b.OwnsMany(o => o.Notes);   // stored in its own table, owned by Order
});

Which one

  • Prefer complex types for value objects in EF Core 8 and later; records with value equality work naturally.
  • Use owned types when you need OwnsMany collections or features your EF Core version does not yet support for complex types.
  • Both are always loaded with their owner; you never Include them.
  • Neither can be queried on its own with a DbSet; they exist only inside their parent.

Interview question

What is the difference between an owned type and a complex type in EF Core?

Both store a class without its own identity inside the parent. Owned types are entity types with a hidden key, tracked by reference, and support collections and JSON mapping. Complex types, added in EF Core 8, are true value types: no key, compared by value, can be records and can be shared between properties, which makes them the better fit for value objects.

Your progress is saved only in this browser.

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