title: Entity Framework insert failed with date column
body: <p>I have a problems with Entity Framework (V 6.1.0), if a DateTime property is annotated as a Date type:</p>
  
  <blockquote>
    <p>Column(TypeName = "Date")</p>
  </blockquote>
  
  <pre><code>public class MyTable
  {
      [DatabaseGenerated]
      public Guid Id { get; set; }
  
      [Key, Column(TypeName = "Date"), DataType(DataType.Date)]
      public DateTime DateKey { get; set; }
  
      [Required]
      public string SomeProperty { get; set; }
  }
  </code></pre>
  
  <p>The column of my table in SQL is created correctly as a DATE column.</p>
  
  <p>Now if I try to insert a new row and set my date property e.g. with DateTime.Now() I get following error:</p>
  
  <blockquote>
    <p>Store update, insert, or delete statement affected an unexpected
    number of rows (0). Entities may have been modified or deleted since
    entities were loaded. Refresh ObjectStateManager entries.</p>
  </blockquote>
  
  <p>The problem is in the generated SQL code by EF:</p>
  
  <pre><code>exec sp_executesql N'
  INSERT [dbo].[MyTable]([DateKey], [SomeProperty]) VALUES (@0, @1)
  
      SELECT [Id] FROM [dbo].[MyTable]
      WHERE @@ROWCOUNT &gt; 0 AND [DateKey] = @0',
  
          N'@0 datetime2(7), @1varchar(max)',
  
          @0='2014-03-26 08:58:07', 
          @1='abcdef'
  </code></pre>
  
  <p>The parameter for DateKey is declared as DATETIME2. I think, This should be DATE.
  This select statement cannot return any row, if there is any time part. If I change parameter @0 to DATE (as it is annotated in my model, I get no error even if time part is still included.</p>
  
  <p>Is this a bug in EF?</p>
  
  <p>Regards,
  Daniel</p>
  
  <p>EDIT:
  I don't know if this is relevant for my question. I'm configured in OnModelCreating all my date column as DATETIME2 by default:</p>
  
  <blockquote>
    <p>modelBuilder.Properties().Configure(p => p.HasColumnType("datetime2"));</p>
  </blockquote>
  
  <p>EDIT on 2014-03-27
  Providing a complete repro:</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations;
      using System.ComponentModel.DataAnnotations.Schema;
      using System.Data.Entity;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
  
      namespace EFDateColumnAsKey
      {
      class Program
      {
          static void Main(string[] args)
          {
              MyContext ctx = new MyContext();
  
              ctx.CalendarItems.Add(new CalendarItem() { StartDate = DateTime.Now.Date });        // This works
              ctx.SaveChanges();
  
              ctx.CalendarItems.Add(new CalendarItem() { StartDate = DateTime.Now });     // This not !!
              ctx.SaveChanges();
  
              ctx.Dispose();
          }
  
          public class MyContext : DbContext
          {
              public DbSet&lt;CalendarItem&gt; CalendarItems { get;set; }
          }
  
  
          public class CalendarItem
          {
              [DatabaseGenerated( System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
              public int Id { get; set; }
  
              [Key, Column(Order = 1), DataType(DataType.Date)]
              public DateTime StartDate { get; set; }
          }
      }
  }
  </code></pre>