title: EF generated SQL query returns empty result when querying datetime column, but the record exists in database
body: <p>I have a very simple SQL Server database table with <code>datetime</code> primary key:</p>
  
  <p><a href="https://i.stack.imgur.com/F1Joo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F1Joo.png" alt="Table with datetime primary key"></a></p>
  
  <p>I generated EF model and database context for this table:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Table("Test")]
  public partial class Test
  {
      public DateTime Id { get; set; }
      public int Value { get; set; }
  }
  
  public partial class Model : DbContext
  {
      public Model() : base("name=Model") { }
      public virtual DbSet&lt;Test&gt; Test { get; set; }
      protected override void OnModelCreating(DbModelBuilder modelBuilder) { }
  }
  </code></pre>
  
  <p>There is a record with <code>Id = 2016-09-21 15:20:01.003</code> in <code>Test</code> table. Let's try to read that record using EF context:</p>
  
  <pre class="lang-cs prettyprint-override"><code>var data = context.Test
      .Where(o =&gt; o.Id == new DateTime(2016, 09, 21, 15, 20, 01, 003))
      .ToList();
  Console.WriteLine(data.Count); // data.Count = 0 here!
  </code></pre>
  
  <p>EF returns 0 records even if the database record with such ID exists. This is a generated EF SQL query:</p>
  
  <pre class="lang-sql prettyprint-override"><code>-- This query produces an empty result
  SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Value] AS [Value]
    FROM [dbo].[Test] AS [Extent1]
    WHERE convert(datetime2, '2016-09-21 15:20:01.0030000', 121) = [Extent1].[Id]
  </code></pre>
  
  <p>As you can see, EF converts <code>DateTime</code> value from the <code>Where</code> method predicate to <code>datetime2</code> SQL type. And indeed, this query returns an empty result set.</p>
  
  <p>If I remove the conversion and change the date to match <code>datetime</code> type format (i.e., remove trailing zeroes), the query works as expected:</p>
  
  <pre class="lang-sql prettyprint-override"><code>-- This query returns existing record
  SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Value] AS [Value]
    FROM [dbo].[Test] AS [Extent1]
    WHERE [Extent1].[Id] = '2016-09-21 15:20:01.003' 
  </code></pre>
  
  <p>I understand that changing my <code>Id</code> column's type to <code>datetime2</code> will solve the problem (actually, Microsoft <a href="https://msdn.microsoft.com/en-us/library/ms187819.aspx" rel="nofollow noreferrer">recommends</a> using <code>datetime2</code> for all new development). But I'm still curious, is this behavior a bug or by design in Entity Framework? Is there a way to make it work with <code>datetime</code> underlying SQL type?</p>