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 'null'. What it means
Thrown by BufferedDataReader.ThrowReadValueException when a non-nullable property/column receives a null (DBNull or NullReferenceException) value from the database during materialization. The message names the expected CLR type to make the null-vs-non-null mismatch obvious.
Solutions
- Make the CLR property nullable (int? / Nullable<T>) to match the database column's nullability.
- Update the database schema to make the column NOT NULL (and backfill existing NULLs).
- If using a database view, ensure the view never returns NULL for that column.
- Configure the property as .IsRequired() only when the column truly cannot be null.
Example fix
// before
public class User
{
public int Id { get; set; }
public int Age { get; set; } // column Age is NULL-able in DB
}
// after
public class User
{
public int Id { get; set; }
public int? Age { get; set; }
} Defensive patterns
Strategy: validation
Validate before calling
static void EnsureNullableClrForNullableColumn(IProperty property, bool columnIsNullable)
{
if (columnIsNullable && !property.IsNullable)
throw new InvalidOperationException($"Property {property.Name} is non-nullable but its column is nullable; NULL reads will fail.");
} Type guard
static bool IsClrNullable(Type t) => !t.IsValueType || Nullable.GetUnderlyingType(t) is not null;
Prevention
- Match CLR nullability to column nullability; use int? for nullable DB columns.
- After schema changes, re-scaffold or update model to reflect column nullability.
- Use .IsRequired() only when the column is truly NOT NULL.
When it happens
Trigger: A column mapped to a non-nullable CLR type (int, bool, Guid, etc.) contains NULL in the database. ThrowReadValueException catches the NullReferenceException from GetFieldValue and reports ErrorMaterializingValueNullReference.
Common situations: Database column allows NULL but entity property is non-nullable (int instead of int?). Stale schema vs model (column added as nullable, then property typed non-nullable). Default value not set on a new NOT NULL column during migration. View/procedure returning NULL for a not-null field.
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…
- The underlying reader doesn't have as many fields as…
- A rows affected parameter, result column or return value…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/9dd12f4f58bb0760.
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)