I have created a ValueObject called Discount as below:
public class Discount:ValueObject
{
public int Value { get; }
public DiscountUnit Unit { get; }
// Code removed for brevity
}
In FluentApi configuration I use the OwnsOne method like this:
builder.OwnsOne(x => x.Discount, b => {
b.Property(x => x.Value)
.IsRequired(true);
b.Property(x => x.Unit)
.HasConversion(s => s.ToString(),
x => (DiscountUnit) Enum.Parse( typeof(DiscountUnit), x))
.IsRequired(true);
} );
When I update the database, Value and Unit are nullable while they are required.