dotnet/efcore · error · InvalidOperationException
Default value '{value}' of type '{valueType}' cannot be set
Error message
Default value '{value}' of type '{valueType}' cannot be set on property '{property}' of type '{propertyType}' in entity type '{entityType}'. What it means
SetDefaultValue converts the supplied value to the property's CLR type via ConvertDefaultValue (RelationalPropertyExtensions.cs:1070, RelationalStrings.IncorrectDefaultValueType). If the value's type is not assignable to the property's CLR type (after UnwrapNullableType) AND Convert.ChangeType fails, EF throws, naming the value, its type, the property, and the expected CLR type.
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 dbf9771522)
Solutions
- Pass a value of a type assignable to property.ClrType (after unwrapping Nullable<T>).
- Convert explicitly before calling: SetDefaultValue(Convert.ChangeType(raw, property.ClrType)).
- Use the typed overload or supply DBNull.Value for SQL NULL, or null for 'no default'.
- For enums, pass the underlying int or a value of the enum type matching the property.
Example fix
// before
modelBuilder.Entity<Order>()
.Property(o => o.CreatedAt) // DateTimeOffset
.HasDefaultValue(DateTime.Now); // DateTime -> throws
// after
modelBuilder.Entity<Order>()
.Property(o => o.CreatedAt)
.HasDefaultValue(DateTimeOffset.Now); // matching CLR type Defensive patterns
Strategy: type-guard
Validate before calling
object defaultValue = /* from config */;
var clrType = property.ClrType.UnwrapNullableType();
if (defaultValue != null && !clrType.IsAssignableFrom(defaultValue.GetType()))
{
defaultValue = Convert.ChangeType(defaultValue, clrType, CultureInfo.InvariantCulture);
}
property.SetDefaultValue(defaultValue); Type guard
static bool DefaultValueAssignable(IReadOnlyProperty p, object? value)
=> value is null || value == DBNull.Value
|| p.ClrType.UnwrapNullableType().IsAssignableFrom(value.GetType()); Prevention
- Always pass a value whose runtime type matches property.ClrType (unwrapped of Nullable<T>).
- For config-driven defaults, Convert.ChangeType to the property CLR type first.
- Use DBNull.Value to mean SQL NULL, null to clear the default.
- Beware enum defaults: pass the enum type or its underlying integral type.
When it happens
Trigger: Passing a value whose type cannot become the property's CLR type, e.g. SetDefaultValue("abc") on an int column, SetDefaultValue(42) on a Guid, SetDefaultValue(DateTime.Now) on a DateTimeOffset without explicit cast, SetDefaultValue(someEnum) where an int is expected.
Common situations: Config-driven default values read from JSON/string config without conversion; copying defaults across properties of differing types in a loop; passing a boxed wrong type.
Related errors
- The type '{givenType}' cannot be mapped as a dictionary beca
- The partition key value supplied for '{propertyType}' proper
- Property '{entityType}.{property}' was configured for full-t
- Invalid token type: '{tokenType}'.
- Got translated node of type '{Result?.GetType().Name ?? "<nu
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/9bc784e63c6abaa6.
Report an issue: GitHub.