title: Using DateTime properties in Code-First Entity Framework and SQL Server
body: <p>I have an example class <code>book</code>:</p>
  
  <pre><code>public class Book
  {
      [Key]
      public int Id { get; set; }
      public string Name { get; set; }
      public DateTime DateAdded { get; set; }
  }
  </code></pre>
  
  <p>When I attempt to add a new <code>book</code> to the <code>BookDb</code> context...</p>
  
  <pre><code>using (BookDb db = new BookDb())
  {
      Book book = new Book {
          Name = "Some name",
          DateAdded = DateTime.Now
      };
  
      db.Books.Add(book);
      db.SaveChanges();
  }
  </code></pre>
  
  <p>... an error is thrown:</p>
  
  <blockquote>
    <p><strong>System.Data.SqlClient.SqlException</strong>: The conversion of a datetime2 data
    type to a datetime data type resulted in an out-of-range value. The
    statement has been terminated.</p>
  </blockquote>
  
  <hr>
  
  <p>I've found that the cause of this is the incompatible <code>datetime</code> types between .NET and SQL Server. There is a way to tell EF to use SQL Server's format in traditional Entity Framework, but <strong>how do I do it in <em>Code-First</em> Entity Framework?</strong></p>
  
  <p>I am using EF4 on .NET 4 (MVC 3 web app) and SQL Server 2008 Express.</p>