dotnet/efcore · error · InvalidOperationException
An error occurred while reading a database value. The…
Error message
An error occurred while reading a database value. The expected type was '{expectedType}' but the actual value was of type '{actualType}'. What it means
Thrown by BufferedDataReader.ThrowReadValueException when the database value cannot be cast to the mapped CLR type (InvalidCastException). The message reports both the expected CLR type and the actual runtime type of the value.
Solutions
- Align the property's CLR type with the actual database column type (e.g. long instead of int).
- Add a value converter if the schema cannot change: .HasConversion<long>(v => v, v => checked((int)v)).
- Update the model from the database (Scaffold-DbContext / dotnet ef dbcontext scaffold) to refresh types.
- Check for provider type-mapping overrides or explicit .HasColumnType that conflict with the column.
Example fix
// before
public class User
{
public int Id { get; set; } // DB column is bigint
}
// after
public class User
{
public long Id { get; set; }
} Defensive patterns
Strategy: validation
Validate before calling
static void EnsureClrTypeMatchesStoreType(IProperty property, string storeType)
{
var expected = property.GetValueConverter()?.ProviderClrType ?? property.ClrType;
// cross-check expected against the database column type via provider mapping
if (storeType.StartsWith("bigint") && expected != typeof(long))
throw new InvalidOperationException($"Property {property.Name} ({expected}) mismatches store type {storeType}.");
} Prevention
- Keep the model in sync with the database schema (re-scaffold after DDL changes).
- Use value converters for unavoidable type gaps.
- Verify provider type mappings for edge types (decimal scale, enums).
When it happens
Trigger: A column's runtime type diverges from the property's CLR type so GetFieldValue throws InvalidCastException; ThrowReadValueException wraps it with ErrorMaterializingValueInvalidCast. E.g. DB returns string but property is int, or returns long for an int property.
Common situations: Schema/model drift: column type changed in DB but not in model. Provider returning a wider type (long vs int, decimal vs double). Implicit conversion gap between database type and CLR type. Stale model after a manual ALTER TABLE. Collation/encoding changes returning byte[] vs string.
Related errors
- An error occurred while reading a database value. The…
- An error occurred while reading a database value. See the…
- The required column ' ' was not present in the results of a…
- An error occurred while reading a database value. The…
- The type ' ' cannot be mapped as a dictionary because it…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/5b0cf0582713b173.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Query/Internal/BufferedDataReader.cs:1934
message = exception is NullReferenceException
|| Equals(value, DBNull.Value)
? RelationalStrings.ErrorMaterializingPropertyNullReference(entityType, propertyName, expectedType)
: exception is InvalidCastException
? CoreStrings.ErrorMaterializingPropertyInvalidCast(entityType, propertyName, expectedType, actualType)
: RelationalStrings.ErrorMaterializingProperty(entityType, propertyName);
}
else
{
message = exception is NullReferenceException
|| Equals(value, DBNull.Value)
? RelationalStrings.ErrorMaterializingValueNullReference(expectedType)
: exception is InvalidCastException
? RelationalStrings.ErrorMaterializingValueInvalidCast(expectedType, actualType)
: RelationalStrings.ErrorMaterializingValue;
}
throw new InvalidOperationException(message, exception);
}
}
}
View on GitHub (pinned to 3a2006ef56)