-
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
Allow non-nullable struct entity property to be DB optional, if conversion to/from nullable type defined. #27227
Comments
For reference, here is the error in EFCore 6.0 that appears when I attempt to specify IsRequired(false) for the aforementioned struct w/ defined conversion to/from string: |
@joschmo80 I'm confused - if the Name property is a non-nullable struct, why do you currently need to have a nullable operator on it? Is the intent for the database column to be nullable or not? |
Thanks for answering this so quickly. The intent is for the database column to be nullable, but the property itself to be non-nullable. I can provide a more specific use case if you desire. |
We have #24685 which is about allowing value converters to change nullability (e.g. non-nullable property with nullable column) - that seems to be what you're looking for (/cc @ajcvickers). Simply allowing IsRequired(false) on non-nullable properties would cause a runtime exception to occur whenever reading a null value, so that's problematic. |
@joschmo80 yes, that's the idea. |
Perfect, thank you very much! And, thank you to the entire EFCore team - your product is amazing! |
Duplicate of #24685 |
Thanks for the kind words @joschmo80! |
@ajcvickers, @roji I have the exact same issue and have tried the solution in #24685. I have an entity, MyEntity with a property defined as follows:
In ConfigureMappings, I am setting
but I get the following exception:
at
Should I expect this to work now or has the support for this been reverted? |
Consider the following example model containing a non-nullable struct:
where PersonNameStruct is an example struct that allows conversion to different formats (Smith, John for display, SMITH^JOHN for storage in medical DICOM files, etc.).
and a conversion defined for PersonNameStruct to/from string (in either OnModelCreating or ConfigureConventions).
It would be nice if the Name property did not have to have a nullable operator (?). This way all code that references that property can avoid having to check against null
(Name.HasValue, Name?.ToString()
,(Name ?? new PersonNameStruct()).ForDisplay, etc.)
Aside from cleaner code, this affects any reflection code that operates on entities.Yes, this can also be solved by making the Name prop NonMapped, and adding a helper prop of type string that IS mapped to the database.
However, this will cause helper props to be littered throughout the Entity classes, which is less than desired.
IsRequired(false)
on non-nullable properties that have a known conversion to/from nullable properties.builder.Properties<PersonNameStruct>().HaveConversion<PersonNameStructConverter>().IsRequired(false);
The text was updated successfully, but these errors were encountered: