{"record":{"id":"b9565f367a61b081","repo":"dotnet/efcore","slug":"an-error-occurred-while-reading-a-database-value-b9565f","errorCode":null,"errorMessage":"An error occurred while reading a database value. See the inner exception for more information.","messagePattern":"An error occurred while reading a database value\\. See the inner exception for more information\\.","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":"Generic catch-all thrown by BufferedDataReader.ThrowReadValueException when the underlying exception during value read is neither a NullReferenceException nor an InvalidCastException. The message is intentionally vague; the real cause is attached as InnerException.","triggerScenarios":"GetFieldValue<T> throws an exception EF does not classify (e.g. FormatException, OverflowException, IOException, provider-specific exception). ThrowReadValueException falls through to ErrorMaterializingValue and wraps the original in InvalidOperationException.","commonSituations":"Numeric overflow (decimal column value too large for the CLR type). FormatException parsing a value (e.g. malformed date string into DateTime). Truncation in the driver. Network/IO error mid-read. Provider-specific conversion bugs.","solutions":["Inspect the InnerException for the real error type and address the root cause (overflow -> widen type, format -> fix data/converter).","Add a value converter to handle the conversion explicitly and tolerate edge values.","Validate/repair the source data so the value is in range and format for the mapped CLR type.","Switch the property to a wider or string CLR type to avoid the conversion failure."],"exampleFix":"// before\npublic class Order\n{\n    public decimal Total { get; set; } // DB has value 1e20, overflow\n}\n\n// after (widen or cap via converter)\npublic class Order\n{\n    public decimal Total { get; set; }\n}\n// or sanitize the data: UPDATE Orders SET Total = 0 WHERE Total > 1e10;","handlingStrategy":"try-catch","validationCode":"static decimal SafeReadDecimal(DbDataReader reader, int ordinal)\n{\n    try { return reader.GetDecimal(ordinal); }\n    catch (OverflowException) { return decimal.MaxValue; } // or log/skip\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    return await query.ToListAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"reading a database value\"))\n{\n    // inspect ex.InnerException for the real cause (Overflow, Format, IO)\n    throw new InvalidOperationException($\"Materialization failed: {ex.InnerException?.Message}\", ex);\n}","preventionTips":["Always inspect InnerException on materialization InvalidOperationExceptions.","Validate source data ranges and formats before reading.","Add value converters that tolerate edge values (overflow, parse errors)."],"tags":["ef-core","query","materialization","inner-exception"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}