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
- Supply a default value whose runtime type matches (or is convertible to) the property's ClrType.
- For enums, pass the enum's underlying numeric value or the strongly-typed enum value.
- Pre-convert the value with Convert.ChangeType before calling SetDefaultValue.
- 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
- Always pass a strongly-typed value matching the property's ClrType.
- For enums, pass the enum value, not a string, unless convertible.
- Use HasDefaultValueSql for non-convertible defaults.
- Add a model test asserting every HasDefaultValue value type-checks.
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
- Both properties ' ' and ' ' on entity type ' ' are mapped…
- The default value has not been specified for the column
- The JSON property name should only be configured on nested…
- A call was made to ' ' that changed an option that must be…
- A call was made to ' ' that changed an option that must be…
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)