-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create check constraints for columns that are only conditionally nullable #20931
Comments
@AndriySvyryd @smitpatel I assume the columns are nullable here because of optional owned types? @pekspro EF Core doesn't do any validation that the entities match the model you define, beyond that which is required to generate correct SQL. This is because doing so is considerable extra overhead in the general case which duplicates validation which must usually be done at higher levels anyway. |
From query perspective, ShippingAddress is null because one of the required column has null value. |
Thanks for a fast response, @ajcvickers and @smitpatel. The fact that EF Core does not validate on save explains a lot. But I am a bit puzzled about:
Does this mean that there is validation when data is read from the database? That is a bit unexpected. Also, |
See #12100 |
We discussed the overall situation where a property is required in the model but the column in the database cannot be nullable. The most common case of this is for TPH, but it may also be applicable to table sharing scenarios. Even though the column can't be nullable, we know it can only be null in certain conditions--for example, when the discriminator column has certain values. We could generate check constraints in the database for this. |
Thanks for you are thinking about this, @ajcvickers . In my real-life scenario where I run into this, I changed my strategy to use base classes and interfaces instead. It was good enough for me :-) |
Related to #2595 |
I'm having an
Order
object, withStreetAddress
as an owned entity.In
OnModelCreating
I have configured bothPostCode
andCity
to be required. But despite that I could add anOrder
withoutCity
and save it successfully into the database (I looks correct in the database too). But if I then read back that orderShippingAddress
will be null.Honestly, I am not sure what to expect in this scenario, but this look weird. If I cannot use required fields on owned entities, I will expect an exception if I try to do that. If it is supported, I should get an exception when I try to save properties with missing values.
Steps to reproduce
You could clone: https://github.com/pekspro/OwnedEntitiesTest
Or just run this code:
Further technical details
EF Core version:3.1.3
Target framework: NET Core 3.1
Just for fun I tried with EF Core version 5.0.0-preview.3.20181.2 but it did behave the same.
The text was updated successfully, but these errors were encountered: