Skip to content

Allow applications to force nullability of columns #24685

Description

@ajcvickers

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.

Activity

  1. self-assigned this
    on Apr 19, 2021
  2. added this to the 6.0.0 milestone on Apr 19, 2021
  3. modified the milestones: 6.0.0, Backlog on May 6, 2021
  4. modified the milestones: Backlog, 7.0.0 on Oct 20, 2021
  5. ajcvickers commented on Oct 21, 2021

    @ajcvickers
    ContributorAuthor

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

  6. AndriySvyryd commented on Oct 21, 2021

    @AndriySvyryd
    Member

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

  7. nettashamir-allocate commented on Feb 9, 2022

    @nettashamir-allocate

    @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?

  8. 11 remaining items

  9. ajcvickers commented on Oct 25, 2022

    @ajcvickers
    ContributorAuthor

    Note from triage: Implement this through #27970.

  10. changed the title [-]Allow value converters to change nullability of columns[/-] [+]Allow applications to force nullability of columns[/+] on Oct 25, 2022
  11. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions