dotnet/efcore · error · InvalidOperationException

An error occurred while reading a database value. The…

Error message

An error occurred while reading a database value. The expected type was '{expectedType}' but the actual value was of type '{actualType}'.

What it means

Thrown by BufferedDataReader.ThrowReadValueException when the database value cannot be cast to the mapped CLR type (InvalidCastException). The message reports both the expected CLR type and the actual runtime type of the value.

Solutions

  1. Align the property's CLR type with the actual database column type (e.g. long instead of int).
  2. Add a value converter if the schema cannot change: .HasConversion<long>(v => v, v => checked((int)v)).
  3. Update the model from the database (Scaffold-DbContext / dotnet ef dbcontext scaffold) to refresh types.
  4. Check for provider type-mapping overrides or explicit .HasColumnType that conflict with the column.

Example fix

// before
public class User
{
    public int Id { get; set; } // DB column is bigint
}

// after
public class User
{
    public long Id { get; set; }
}
Defensive patterns

Strategy: validation

Validate before calling

static void EnsureClrTypeMatchesStoreType(IProperty property, string storeType)
{
    var expected = property.GetValueConverter()?.ProviderClrType ?? property.ClrType;
    // cross-check expected against the database column type via provider mapping
    if (storeType.StartsWith("bigint") && expected != typeof(long))
        throw new InvalidOperationException($"Property {property.Name} ({expected}) mismatches store type {storeType}.");
}

Prevention

When it happens

Trigger: A column's runtime type diverges from the property's CLR type so GetFieldValue throws InvalidCastException; ThrowReadValueException wraps it with ErrorMaterializingValueInvalidCast. E.g. DB returns string but property is int, or returns long for an int property.

Common situations: Schema/model drift: column type changed in DB but not in model. Provider returning a wider type (long vs int, decimal vs double). Implicit conversion gap between database type and CLR type. Stale model after a manual ALTER TABLE. Collation/encoding changes returning byte[] vs string.

Related errors


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