Entity Framework Core Owned Entities - gecko-8/devwiki GitHub Wiki
An Owned Entity is an Entity that is "embedded" within another. Basically it's a way of grouping properties into logical classes but still having them combined into a single table in the database.
Here's an example of using an Owned Entity to modularize address information into a user entity:
using Microsoft.EntityFrameworkCore;
namespace NestedEntity.Model
{
public class User
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public Location Location { get; set; } = new Location();
}
[Owned]
public class Location
{
public string Address { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
}
}
This will result in a User table with the following columns:
- Id
- Name
- Location_Address
- Location_City
This can also be done with the Fluent API:
modelBuilder.Entity<User>().OwnsOne(p => p.Location);