title: How can the type of SQL query parameter be controlled when using a string property in .NET Entity Framework
body: <p>On the database table there is a column of type <code>char(2)</code>.</p>
  
  <p>When I use a parameter in linq such as <code>.Where(x =&gt; x.Code == code)</code> the SQL query parameter is translated into <code>varchar(max)</code> where I would really like it to be <code>char(2)</code> 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.</p>
  
  <pre><code>Property(c =&gt; c.Code)
              .HasColumnType("char")
              .IsFixedLength()
              .HasMaxLength(2);
  </code></pre>
  
  <p>The code first mapping is shown above. The only thing that seems to affect the translation is the <code>.HasColumnType("char")</code> It doesn't seem to affect the query translation, as that influences whether it picks a <code>varchar(max)</code> or <code>nvarchar(max)</code>.</p>
  
  <p>I have thought of two solutions, one is to get the database changed so that it uses an <code>int</code> 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.</p>