title: Entity Framework 5 wrong data type in query
body: <p>We are using EF 5.0 as our ORM of choice in our business solution, structured in a n-layer fashion with everything decoupled and a nice composition root with ninject.</p>
  
  <p>Lately, we've been building a database that uses partitioning underneath, and we have some important indexes on <code>DATE</code> columns.</p>
  
  <p>The columns are correctly declared on Sql Server 2008. We also added the correct data type in the EF mappings, with the <code>HasColumnType("Date")</code> instruction.</p>
  
  <p>Still, when querying the table through Linq to Entities, the parameters that we filter the dates on are created of type <code>DateTime2</code> and even the columns are cast to <code>DateTime2</code> in the queries so the type matches the parameters.</p>
  
  <p>This behaviour has several problems. First of all, if I'm telling EF engine that the column on the database is <code>DATE</code> why should it cast it to <code>DateTime2</code>?</p>
  
  <p>Second, this cast is making the database ignore the indexes, thus not using partitioning. We have one year per phisical partitioning, and if I ask a date range, let's say, february 2013 to march 2013 the scan should happend only on one physical partition. It works correctly if manually using the correct data type <code>DATE</code> but with the cast to <code>DateTime2</code> all the partitions get scanned, reducing performance drastically.</p>
  
  <p>Now, I'm sure I'm missing out something, because it would be rather stupid that Microsoft ORM doesn't work well on Microsoft Sql Server.</p>
  
  <p>I've been unable to find any documentation on how have EF use the correct data types in queries, so I'm asking here. Any help will be appreciated.</p>
  
  <p>Thanks.</p>