dotnet/efcore · error · InvalidOperationException

An error occurred while reading a database value. See the in

Error message

An error occurred while reading a database value. See the inner exception for more information.

What it means

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.

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 dbf9771522)

Solutions

  1. Inspect the InnerException for the true cause and address it (e.g. widen the property type, fix data, update provider).
  2. If the value is out of range, change the CLR property type or apply HasConversion to a larger type.
  3. Wrap the read in a try/catch if the column can legitimately contain bad data, and log/skip the row.
  4. Update the database provider / ADO.NET driver to fix provider-specific decoding bugs.

Example fix

// before - DB Money value overflows decimal on some rows
public decimal Balance { get; set; }

// after - read defensively or widen
try { var e = ctx.Accounts.FromSqlRaw(sql).ToList(); }
catch (InvalidOperationException ex)
{ _logger.LogError(ex.InnerException, "read failed"); }
// or model the column with enough precision/scale
Defensive patterns

Strategy: try-catch

Validate before calling

// run a probe query for rows that could overflow/throw, e.g. narrow numeric ranges
var suspect = await ctx.Database.SqlQueryRaw<int>("SELECT COUNT(*) FROM Accounts WHERE Balance > 999999999").ToListAsync();

Try / catch

try { return ctx.Set<T>().FromSqlRaw(sql).ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("An error occurred while reading a database value"))
{ _logger.LogError(ex.InnerException ?? ex, "Materialization failed; inspect inner exception."); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/b9565f367a61b081. Report an issue: GitHub.