{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/BufferedDataReader.cs#L1916-L1938","documentation":"Generic materialization failure: ThrowReadValueException rethrows an InvalidOperationException with 'An error occurred while reading a database value' when the underlying reader.GetFieldValue threw an exception that is neither a NullReferenceException/DBNull nor an InvalidCastException. The original exception is attached as InnerException for diagnosis.","triggerScenarios":"A provider-level error while reading a value (e.g. overflow, format, connection drop, truncated data, provider-specific decoding error); a custom GetFieldValue delegate in a ReaderColumn<T> throwing an arbitrary exception.","commonSituations":"Database-side data corruption/truncation; provider bugs; network blips during streaming; custom value converters that throw during read; numeric overflow when the DB value exceeds the CLR type's range.","solutions":["Inspect the InnerException for the true cause and address it (e.g. widen the property type, fix data, update provider).","If the value is out of range, change the CLR property type or apply HasConversion to a larger type.","Wrap the read in a try/catch if the column can legitimately contain bad data, and log/skip the row.","Update the database provider / ADO.NET driver to fix provider-specific decoding bugs."],"exampleFix":"// before - DB Money value overflows decimal on some rows\npublic decimal Balance { get; set; }\n\n// after - read defensively or widen\ntry { var e = ctx.Accounts.FromSqlRaw(sql).ToList(); }\ncatch (InvalidOperationException ex)\n{ _logger.LogError(ex.InnerException, \"read failed\"); }\n// or model the column with enough precision/scale","handlingStrategy":"try-catch","validationCode":"// run a probe query for rows that could overflow/throw, e.g. narrow numeric ranges\nvar suspect = await ctx.Database.SqlQueryRaw<int>(\"SELECT COUNT(*) FROM Accounts WHERE Balance > 999999999\").ToListAsync();","typeGuard":null,"tryCatchPattern":"try { return ctx.Set<T>().FromSqlRaw(sql).ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"An error occurred while reading a database value\"))\n{ _logger.LogError(ex.InnerException ?? ex, \"Materialization failed; inspect inner exception.\"); throw; }","preventionTips":["Inspect InnerException for the real cause and fix at the source.","Keep provider drivers up to date.","Widen property types when data ranges grow."],"tags":["efcore","query","materialization","datareader","provider"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}