Using Owned Types in EF Core for Value Objects
A practical guide to using EF Core owned types for value objects, covering Fluent API configuration, migration verification, expected checks, and recovery options.
03 Nov 2025, 09:05 UTC

Desired Outcome and Immediate Takeaway
\nWhen you model a value object in Entity Framework Core that must share the primary key of its parent, disappear when the parent is deleted, and avoid a separate database table, the owned type feature is the supported path. This article walks through configuring an owned type, verifying the resulting schema, and recognizing the practical limits so you can decide if it fits your data model.
\n\nPrerequisites
\n- \n
- .NET 6 or later (EF Core 6+ owned type support is stable; earlier versions have limitations) \n
- A DbContext-derived class in your project \n
- The EntityFrameworkCore NuGet package (version 8.x at time of writing) \n
- Basic familiarity with migrations and the Fluent API \n
Configuring an Owned Type via the Fluent API
\nOwned types are value-type entities that share the primary key of the owning entity. They are configured using the OwnsOne method in the Fluent API or the Owned attribute. Below is a minimal example.
\npublic class Order { public int Id { get; set; } public string Status { get; set; } public CustomerDetails CustomerDetails { get; set; } } public class CustomerDetails { public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } } public class MyDbContext : DbContext { public DbSet Orders { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().OwnsOne(o => o.CustomerDetails); } }\nThe OwnsOne call tells EF Core that CustomerDetails is an owned type. Its columns (FirstName, LastName, Email) are added to the Orders table. No separate CustomerDetails table is created. The owned property is removed from the database when the owning Order row is deleted, because the default cascade behavior is enabled.
\n\nGenerating and Verifying the Migration
\nAfter defining the owned type, create a migration to materialize the schema change.
\ndotnet ef migrations add AddOwnedCustomerDetails --project ./MyProject --startup-project ./MyProject.Api\nRun the command from a terminal in the directory containing the project file. You need write access to the project folder and a valid connection string in appsettings.json; the command will fail if the project cannot be resolved or the database is unreachable.
\nOpen the generated migration file. You should see the OwnsOne configuration translated into column definitions within the Orders table, not a new table creation. Apply the migration:
\ndotnet ef database update --project ./MyProject --startup-project ./MyProject.Api\nUse a database client (e.g., sqlcmd, pgcli, or DB Browser for SQLite) to inspect the Orders table schema. The owned columns appear directly after the owner's columns, and no separate owned table exists.
\n\nExpected Behavioral Checks
\n- \n
- Query navigation: Query an Order and access order.CustomerDetails.FirstName; EF Core executes a simple SELECT on the Orders table because the data is co-located. No outer join is generated. \n
- Cascade delete: Delete an Order entity and call context.SaveChanges(); verify that no orphan CustomerDetails rows remain in the database. The owned rows are cascade-deleted automatically. \n
- Independent query attempt: Try to query CustomerDetails directly via context.Set(); this will produce no results or throw because the type is not mapped as an independent entity. \n
Limitations, Recovery, and Migration Rollback
\nOwned types come with constraints that affect when they are the right choice.
\n- \n
- No independent primary key: Attempting to define a key on the owned type using .HasKey() in the Fluent API throws an exception. If your domain requires a separate identity, model the relationship as a one‑to‑one entity instead. \n
- No lazy loading: Navigation property access always hits the database immediately. Owned types do not participate in EF Core's lazy loading pipeline. \n
- Tracking scope: Changes to owned properties are tracked only while the owner is attached to a context. Detach the owner, modify the owned object, and re‑attach to avoid lost updates. \n
- Rollback considerations: If a migration that added owned columns needs to be reversed, use the standard dotnet ef migrations remove command. Because owned columns are part of the owner's table, dropping them requires careful data preservation planning—back up existing data before running a rollback. \n
Comparison: Owned Type vs. Separate One‑to‑One Entity
\n| Aspect | \nOwned Type | \nSeparate Entity (one‑to‑one) | \n
|---|---|---|
| Primary key | \nShared with owner | \nIndependent | \n
| Separate table | \nNo | \nYes | \n
| Cascade delete | \nAutomatic | \nConfigurable (usually foreign key) | \n
| Independent query | \nNot supported | \nSupported | \n
Owned types give you a concise way to model value objects without extra tables, provided you accept the shared‑primary‑key constraint and the query‑joins behavior. Evaluate your domain's identity requirements before committing.
\n0 replies
A thoughtful contribution can make all the difference. Be the first to share one.