Class OwnedEntityAttribute
- Namespace
- Trellis.EntityFrameworkCore
- Assembly
- Trellis.EntityFrameworkCore.dll
Marks a composite ValueObject as an EF Core owned entity type, triggering source generation of the private parameterless constructor required for EF Core materialization.
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class OwnedEntityAttribute : Attribute
- Inheritance
-
OwnedEntityAttribute
- Inherited Members
- Extension Methods
Examples
[OwnedEntity]
public partial class Address : ValueObject
{
public string Street { get; private set; }
public string City { get; private set; }
public string State { get; private set; }
public Address(string street, string city, string state)
{
Street = street;
City = city;
State = state;
}
protected override IEnumerable<IComparable?> GetEqualityComponents()
{
yield return Street;
yield return City;
yield return State;
}
}
// Generator emits:
// partial class Address
// {
// private Address()
// {
// Street = null!;
// City = null!;
// State = null!;
// }
// }
Remarks
The decorated type must be partial and inherit from ValueObject.
The source generator emits a private parameterless constructor that initializes all
reference-type properties with null! to satisfy the compiler's nullability analysis.
Properties should be settable so EF Core can populate them during materialization. Use { get; private set; } as the supported, tested pattern.
Note — init-only properties. { get; init; } on properties of
[OwnedEntity] types is not covered by Trellis tests today and is therefore not
guaranteed to round-trip. Use { get; private set; }, which is the supported shape.
The TRLS022 analyzer flags { get; init; } properties on [OwnedEntity] types.