title: Nullable DateTime property in Entity Framework 6 thows exception on save when value is null
body: <p>I have defined a Nullable <code>DateTime</code>property in one of my classes:</p>
  
  <pre><code>public DateTime? ControlDate { get; set; }
  </code></pre>
  
  <p>When using EF 6 CodeFirst to generate the database (SQL Server 2008) from my model I get:</p>
  
  <p><img src="https://i.stack.imgur.com/p1ucc.png" alt="ControlDate null"></p>
  
  <p>The problem is that when I save my instance with <code>ControlDate=null</code> I get this exception:</p>
  
  <pre><code>conversion of a datetime2 data type to a datetime data type
  resulted in an out-of-range value
  </code></pre>
  
  <p>I have read multiple related posts and articles saying that this usually happens when you 
  define a Non-nullable DateTime property and try to save it without setting a valid date before,
  and some people suggest setting the property as nullable in case the property value can be null (which is my particular case). </p>
  
  <p>My question is: why is EF trying to set a default date when my property and column type are nullable. Null should be a valid value and should flow all the way to the database without any other conversion in between.</p>
  
  <p>Here a related article: <a href="https://stackoverflow.com/questions/1331779/c-sharp-conversion-of-a-datetime2-data-type-to-a-datetime-data-type">Conversion of a datetime2 data type to a datetime data type results out-of-range value</a></p>
  
  <p>EDIT: : <a href="https://stackoverflow.com/questions/14246426/why-does-ef5-code-first-use-datetime2-when-inserting-a-nullable-datetime-into-th">Here is a very similar question</a>. very detailed explanation, in case anyone is interested.</p>
  
  <p><strong>Lessons Learned:</strong>  Just wanted to clarify that after looking closer, I figured out it was an issue on my side. Right before saving my object it was being set to: </p>
  
  <pre><code>myObject.ControlDate = new DateTime()
  </code></pre>
  
  <p>Which, while inspected, it displayed the default incompatible date <code>1/1/0001</code>. Which is well known to cause this exception. So my conclusions:</p>
  
  <ul>
  <li>A model declaring a non-nullable <code>DateTime</code> property will result in a <code>datetime</code>data type in SQL Server. </li>
  <li>An instance of a class declaring a nullable <code>DateTime</code>property will be able to save to the DB as null</li>
  <li><strong>It is very important</strong> to set a valid default date manually (in case &lt;> NULL), otherwise it will set it to 1/1/0001 and throw the exception</li>
  </ul>