dotnet/efcore · error · InvalidOperationException

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

Error message

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

What it means

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.

Solutions

  1. Inspect the InnerException for the real error type and address the root cause (overflow -> widen type, format -> fix data/converter).
  2. Add a value converter to handle the conversion explicitly and tolerate edge values.
  3. Validate/repair the source data so the value is in range and format for the mapped CLR type.
  4. Switch the property to a wider or string CLR type to avoid the conversion failure.

Example fix

// before
public class Order
{
    public decimal Total { get; set; } // DB has value 1e20, overflow
}

// after (widen or cap via converter)
public class Order
{
    public decimal Total { get; set; }
}
// or sanitize the data: UPDATE Orders SET Total = 0 WHERE Total > 1e10;
Defensive patterns

Strategy: try-catch

Validate before calling

static decimal SafeReadDecimal(DbDataReader reader, int ordinal)
{
    try { return reader.GetDecimal(ordinal); }
    catch (OverflowException) { return decimal.MaxValue; } // or log/skip
}

Try / catch

try
{
    return await query.ToListAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("reading a database value"))
{
    // inspect ex.InnerException for the real cause (Overflow, Format, IO)
    throw new InvalidOperationException($"Materialization failed: {ex.InnerException?.Message}", ex);
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/b9565f367a61b081. 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)