{"record":{"id":"9dd12f4f58bb0760","repo":"dotnet/efcore","slug":"an-error-occurred-while-reading-a-database-value-9dd12f","errorCode":null,"errorMessage":"An error occurred while reading a database value. The expected type was '{expectedType}' but the actual value was 'null'.","messagePattern":"An error occurred while reading a database value\\. The expected type was '(.+?)' but the actual value was 'null'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/Internal/BufferedDataReader.cs","lineNumber":1934,"sourceCode":"\n                message = exception is NullReferenceException\n                    || Equals(value, DBNull.Value)\n                        ? RelationalStrings.ErrorMaterializingPropertyNullReference(entityType, propertyName, expectedType)\n                        : exception is InvalidCastException\n                            ? CoreStrings.ErrorMaterializingPropertyInvalidCast(entityType, propertyName, expectedType, actualType)\n                            : RelationalStrings.ErrorMaterializingProperty(entityType, propertyName);\n            }\n            else\n            {\n                message = exception is NullReferenceException\n                    || Equals(value, DBNull.Value)\n                        ? RelationalStrings.ErrorMaterializingValueNullReference(expectedType)\n                        : exception is InvalidCastException\n                            ? RelationalStrings.ErrorMaterializingValueInvalidCast(expectedType, actualType)\n                            : RelationalStrings.ErrorMaterializingValue;\n            }\n\n            throw new InvalidOperationException(message, exception);\n        }\n    }\n}\n","sourceCodeStart":1916,"sourceCodeEnd":1938,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/Internal/BufferedDataReader.cs#L1916-L1938","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic class User\n{\n    public int Id { get; set; }\n    public int Age { get; set; } // column Age is NULL-able in DB\n}\n\n// after\npublic class User\n{\n    public int Id { get; set; }\n    public int? Age { get; set; }\n}","handlingStrategy":"validation","validationCode":"static void EnsureNullableClrForNullableColumn(IProperty property, bool columnIsNullable)\n{\n    if (columnIsNullable && !property.IsNullable)\n        throw new InvalidOperationException($\"Property {property.Name} is non-nullable but its column is nullable; NULL reads will fail.\");\n}","typeGuard":"static bool IsClrNullable(Type t) => !t.IsValueType || Nullable.GetUnderlyingType(t) is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["ef-core","query","materialization","nullability"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}