efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Exception message for type mismatches

Open ankane opened this issue 9 months ago • 1 comments

What problem are you trying to solve?

Hi, when specifying a CLR type that does not match the SQL type, like:

public class Item
{
    [Column(TypeName = "int")]
    public string? Count { get; set; }
}

EF Core throws a System.InvalidOperationException with the message:

The property 'Item.Count' could not be mapped because it is of type 'string', which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

However, string is a supported type, so this message seems a bit confusing (this applies to all types, string is just an example).

Describe the solution you'd like

An error message that mentions a type mismatch.

ankane avatar Mar 26 '25 06:03 ankane

Yeah, this seems like the generic message we throw when the provider's type mapping source fails to find a mapping (returns null). We should tweak the wording here.

roji avatar Mar 26 '25 20:03 roji