Persist value objects as owned entity types in EF Core 2.0 and later Even with some gaps between the canonical value object pattern in DDD and the owned entity type in
EF Core, it’s currently the best way to persist value objects with E
F Core 2.0 and later. You can see
limitations at the end of this section.
The owned entity type feature was added to EF Core since version 2.0.
An owned entity type allows you to map types that do not have their own identity explicitly defined in
the domain model and are used as properties, such as a value object, within any of your entities. An
owned entity type shares the same CLR type with another entity type (that is, it’s just a regular class).
The entity containing the defining navigation is the owner entity. When querying the owner, the
owned types are included by default.
Just by looking at the domain model, an owned type looks like it doesn’t have any identity. However,
under the covers, owned types do have the identity, but the owner navigation property is part of this
identity.
The identity of instances of owned types is not completely their own. It consists of three components:
•
The identity of the owner
•
The navigation property pointing to them
•
In the case of collections of owned types, an independent component (supported in EF Core 2.2
and later).
For example, in the Ordering domain model at eShopOnContainers, as part of the Order entity, the
Address value object is implemented as an owned entity type within the owner entity, which is the
Order entity.
Address
is a type with no identity property defined in the domain model. It is used as a
property of the Order type to specify the shipping address for a particular order.
By convention, a shadow primary key is created for the owned type and it will be mapped to the same
table as the owner by using table splitting. This allows to use owned types similarly to how complex
types are used in EF6 in the traditional .NET Framework.
It is important to note that owned types are never discovered by convention in EF Core, so you have
to declare them explicitly.
In eShopOnContainers, in the OrderingContext.cs file, within the
OnModelCreating()
method,
multiple infrastructure configurations are applied. One of them is related to the Order entity.
// Part of the OrderingContext.cs class at the Ordering.Infrastructure project
//