EntityFramework.Docs
EntityFramework.Docs copied to clipboard
How to "... mark the incoming navigation as required to always create an instance"?
At https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes#old-behavior it reads:
System.InvalidOperationException: Entity type 'ContactInfo' is an optional dependent using table sharing and containing other dependents without any required non shared property to identify whether the entity exists. If all nullable properties contain a null value in database then an object instance won't be created in the query causing nested dependent's values to be lost. Add a required property to create instances with null values for other properties or mark the incoming navigation as required to always create an instance.
Can't you show an example of how to achieve "mark the incoming navigation as required to always create an instance", please?
For instance this does not work, because the IsRequired() method does not exist:
builder.OwnsOne(p => p.Value, ot =>
{
ot.Property(d => d.Street);
ot.Property(p => p.Location).;
ot.Property(p => p.PostalCode);
ot.Property(p => p.City);
ot.Property(p => p.Country);
}).IsRequired();
I have the same question. Is there a way to mark the navigation property as required?
@jp2masa @ajcvickers In fact I have figured out how to do it, i.e. like:
builder.OwnsOne(p => p.Value, ot => { });
builder.Navigation(p => p.Value).IsRequired();
So to me it seems like specifying OwnsOne and IsRequired is nearly completely unrelated. I would find it more reasonable, if it would be specified as I wrote in my initial post. I would also find it reasonable, if there would have been a reference in the documentation on OwnsOne to the other segment in the documentation where this is described, or perhaps the example could just include the usage of IsRequired().
It works, thanks!