You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use value objects to encapsulate validations like maxLength. This is an extract of a sample class without factory method and validation:
public class CallingName : ValueObject
{
public string Value { get; }
public const int MaxLength = 50;
private CallingName(string text)
{
Value = text;
}
...
}
In the SQL database the table should have a field 'CallingName' of type nvarchar with a maximum length of 50.
Using EF Core I'm aware of value converters and owned types, but I don't fully understand the options and differences between both. If a value object has only one property, is it possible to use either one?
entity.OwnsOne(e => e.CallingName,
p => p.Property(e=>e.Value)
.HasColumnName("CallingName")
.HasMaxLength(CallingName.MaxLength));
Do all of the above version work? What is the purpose of the different versions? What are the cases or criteria to use a value converter or an owned type?
To my understanding, when types have no public setters they need an empty constructor for owned types. Value converters can call the constructor, resp. the factory method. Is that correct?
The documentation for OwnsOne shows 6 overloads, but has no examples. The descriptions sound Klingon to me. I don't understand what each one is for. I couldn't find any better explantion or article googling. Is there a good explanation with examples?
Include provider and version information
EF Core version:
Microsoft.EntityFrameworkCore.SqlServer
.NET 6
The text was updated successfully, but these errors were encountered:
@KillerBoogie Owned types are entity types that have a key property and are identified by the value of that key. (EF may hide this key value from you, but it is still there and affects the semantics of the how the objects are used.) In DDD terms, they are intended to support aggregates, with the owner acting as the aggregate root. They do not work very well for DDD value objects since they are identified by a key, which goes against the principle of value objects.
Using a value converter means that the property is still mapped as a property, and does not have a key. This means that properties with a value converter inherently have value object semantics, and so are a much better fit than owned types.
Thank you for that clear description! To my understanding Owned Types are the only option for value objects with more than one property until EF Core 7.
Do you know of any explantion of the OwnsOne overloads and why to use each one?
@KillerBoogie They generally all do the same thing, but take different forms of parameters, and optionally allow a delegate for further configuration. Examples are provided in the conceptual docs for owned types.
I use value objects to encapsulate validations like maxLength. This is an extract of a sample class without factory method and validation:
In the SQL database the table should have a field 'CallingName' of type
nvarchar
with a maximum length of 50.Using EF Core I'm aware of value converters and owned types, but I don't fully understand the options and differences between both. If a value object has only one property, is it possible to use either one?
So far I used a value converter for the field:
Looking at the simple example in the Microsoft docs I would think that this should work without a value converter like this
or
or
Do all of the above version work? What is the purpose of the different versions? What are the cases or criteria to use a value converter or an owned type?
To my understanding, when types have no public setters they need an empty constructor for owned types. Value converters can call the constructor, resp. the factory method. Is that correct?
The documentation for OwnsOne shows 6 overloads, but has no examples. The descriptions sound Klingon to me. I don't understand what each one is for. I couldn't find any better explantion or article googling. Is there a good explanation with examples?
Include provider and version information
EF Core version:
Microsoft.EntityFrameworkCore.SqlServer
.NET 6
The text was updated successfully, but these errors were encountered: