Skip to content

Extensibility for getting the default column value for a type mapping (for adding non-nullable columns) #34790

@roji

Description

@roji

When adding a non-nullable column to an existing table, a default value must be provided, otherwise the ALTER TABLE fails. We have various hard-coded hacks to deal with this scenario: MigrationsModelDiffer.GetDefaultValue() has specific handling for strings and arrays, and returns the default CLR value otherwise:

protected virtual object? GetDefaultValue(Type type)
    => type == typeof(string)
        ? string.Empty
        : type.IsArray
            ? Array.CreateInstance(type.GetElementType()!, 0)
            : type.UnwrapNullableType().GetDefaultValue();

This is wrong for reference types such as List<string> and probably NetTopologySuite things as well.

We also have hacky differ code here, which specifically identifies CollectionToJsonStringConverter to set [] for JSON columns.

I think we simply need a hook on our type mapping which provides this.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions