dotnet/efcore · error · InvalidOperationException

Default value ' ' of type ' ' cannot be set on property ' '…

Error message

Default value '{value}' of type '{valueType}' cannot be set on property '{property}' of type '{propertyType}' in entity type '{entityType}'.

What it means

When setting a default value on a property, EF Core checks that the value's runtime type is assignable to the property's ClrType. If not, it attempts Convert.ChangeType as a fallback; if that also throws, EF Core reports the mismatch with the value, its type, the property name, the expected ClrType, and the declaring entity type.

Solutions

  1. Supply a default value whose runtime type matches (or is convertible to) the property's ClrType.
  2. For enums, pass the enum's underlying numeric value or the strongly-typed enum value.
  3. Pre-convert the value with Convert.ChangeType before calling SetDefaultValue.
  4. If you need a SQL-side default expression instead, use HasDefaultValueSql.

Example fix

// before
modelBuilder.Entity<User>().Property(u => u.Age).HasDefaultValue("twenty-one");
// after
modelBuilder.Entity<User>().Property(u => u.Age).HasDefaultValue(21);
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate the default value type before assigning.
var expected = property.ClrType.UnwrapNullableType();
var actual = value.GetType();
if (!expected.IsAssignableFrom(actual)
    && !(expected.IsEnum && expected.GetEnumUnderlyingType() == actual)
    && !IsConvertible(actual, expected))
{
    throw new InvalidOperationException($"Default value type {actual} is not compatible with {expected}.");
property.SetDefaultValue(value);

static bool IsConvertible(Type from, Type to)
    => TypeDescriptor.GetConverter(from).CanConvertTo(to);

Type guard

static bool IsCompatibleDefaultValue(Type clrType, object? value)
    => value is null
       || clrType.UnwrapNullableType().IsAssignableFrom(value.GetType())
       || (clrType.UnwrapNullableType().IsEnum
           && clrType.UnwrapNullableType().GetEnumUnderlyingType() == value.GetType());

Prevention

When it happens

Trigger: Calling property.SetDefaultValue(value) / HasDefaultValue(value) where value's type is neither assignable to the property's ClrType nor convertible via Convert.ChangeType (e.g. a string default on an int column that is not numeric, or a complex object on a scalar column).

Common situations: Passing a boxed value of the wrong type (e.g. "abc" for an int column); using enums with mismatched underlying types; timezone/DateTimeOffset mismatches; config generated from external metadata with inconsistent types.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/9bc784e63c6abaa6. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs:1070

    private static object? ConvertDefaultValue(IReadOnlyProperty property, object? value)
    {
        if (value == null
            || value == DBNull.Value)
        {
            return value;
        }

        var valueType = value.GetType();
        if (!property.ClrType.UnwrapNullableType().IsAssignableFrom(valueType))
        {
            try
            {
                return Convert.ChangeType(value, property.ClrType, CultureInfo.InvariantCulture);
            }
            catch
            {
                throw new InvalidOperationException(
                    RelationalStrings.IncorrectDefaultValueType(
                        value, valueType.ShortDisplayName(), property.Name, property.ClrType, property.DeclaringType.DisplayName()));
            }
        }

        return value;
    }

    /// <summary>
    ///     Gets the <see cref="ConfigurationSource" /> for the default value.
    /// </summary>
    /// <param name="property">The property.</param>
    /// <returns>The <see cref="ConfigurationSource" /> for the default value.</returns>
    public static ConfigurationSource? GetDefaultValueConfigurationSource(this IConventionProperty property)
        => property.FindAnnotation(RelationalAnnotationNames.DefaultValue)?.GetConfigurationSource();

    /// <summary>
    ///     Gets the maximum length of data that is allowed in this property. For example, if the property is a <see cref="string" />

View on GitHub (pinned to 3a2006ef56)