On the database table there is a column of type char(2).
When I use a parameter in linq such as .Where(x => x.Code == code) the SQL query parameter is translated into varchar(max) where I would really like it to be char(2) in order to avoid an implicit conversion. This is because the query is not using the indexes of the database in the way that I would like.
Property(c => c.Code)
.HasColumnType("char")
.IsFixedLength()
.HasMaxLength(2);
The code first mapping is shown above. The only thing that seems to affect the translation is the .HasColumnType("char") It doesn’t seem to affect the query translation, as that influences whether it picks a varchar(max) or nvarchar(max).
I have thought of two solutions, one is to get the database changed so that it uses an int and a lookup instead of the char code, the other is to convert the code to use an expression. Two things I’d rather not do, if there is a better way to control the mappings to sql.
I worked out a way to achieve this.
Instead of using
.Where(x => x.Code = code)I am now using
…
This translates into a constant in the query rather than a parameter.