Skip to content
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 applications to force nullability of columns #24685

Open
Tracked by #22952
ajcvickers opened this issue Apr 19, 2021 · 6 comments
Open
Tracked by #22952

Allow applications to force nullability of columns #24685

ajcvickers opened this issue Apr 19, 2021 · 6 comments
Assignees
Labels
area-type-mapping consider-for-current-release punted-for-6.0 punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Member

Split off from #13850

By default, a column is nullable in the database if the property to which it is mapped is nullable. This is true even if the converter itself never converts nulls, since by default nulls are passed through unconverted. However, if the value converter converts nulls and ensures that no null values result from this conversion, then it it can make sense for the database column to be non-nullable.

@ajcvickers
Copy link
Member Author

Note from triage: Ping @AndriySvyryd when working on this.

@AndriySvyryd
Copy link
Member

Add SetIsNullable(this IMutableProperty, bool, in StoreObjectIdentifier) to allow configuring column nullability independently from property nullability.

@nettashamir-allocate
Copy link

@ajcvickers I have the exact same issue as in [#27227] (#27227) and have tried to use this as the solution but no joy.

I have an entity, MyEntity with a property defined as follows:

NullableTime NonNullableButNullInDB { get; set; }

NullableTime is a special type class which expects to be always populated in the model but may have a null DateTime associated with it in the DB. The existence and usage of this class is legacy and I can't get rid of it at the moment so have to be able to support it.

In ConfigureMappings, I am setting

protected override void ConfigureMappings(EntityTypeBuilder<MyEntity> entity) {
   entity.Property(e => e.NonNullableButNullInDB).Metadata.IsNullable = true;

but I get the following exception:

The property 'MyEntity.NonNullableButNullInDB' cannot be marked as nullable/optional because the type of the property is 'NullableTime' which is not a nullable type. Any property can be marked as non-nullable/required, but only properties of nullable types can be marked as nullable/optional.

at

   at Microsoft.EntityFrameworkCore.Metadata.Internal.Property.SetIsNullable(Nullable`1 nullable, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.Property.set_IsNullable(Boolean value)
   at MyEntityConfiguration.ConfigureMappings(EntityTypeBuilder`1 entity)

Should I expect this to work now or has the support for this been reverted?

@ajcvickers
Copy link
Member Author

@nettashamir-allocate This is the issue tracking the scenario you describe.

@AndriySvyryd
Copy link
Member

Note to implementer: the main usage to be updated is

@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 11, 2022
@AndriySvyryd AndriySvyryd removed their assignment Aug 11, 2022
@ajcvickers ajcvickers added the punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. label Aug 14, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Aug 14, 2022
@ajcvickers ajcvickers removed the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 14, 2022
@ajcvickers
Copy link
Member Author

Note from triage: Implement this through #27970.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-type-mapping consider-for-current-release punted-for-6.0 punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants