title: Entity Framework: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value C#
body: <p>I have a class Announcment:</p>
  
  <pre><code>namespace WebApplication2.Models {
  
      public partial class Announcment {
          public int Id { get; set; }
  
          [DisplayName("Subject")]
          public String Subject { get; set; }
  
          [DataType(DataType.MultilineText)]
          [DisplayName("Content")]
          public String Text { get; set; }
  
          [DisplayName("Published")]
          public DateTime TimeStamp { get; set; }
  
          [DisplayName("Author")]
          public virtual ApplicationUser Author { get; set; }
  
      }
  }
  </code></pre>
  
  <p>and I create it like this. The date <code>TimeStamp</code> is set automatically to <code>DateTime.Now</code>:</p>
  
  <pre><code> [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(Include = "Id,Subject,Text")] Announcment announcment) {
              if (ModelState.IsValid) {
                  //MINE 
                  var user = UserManager.FindById(User.Identity.GetUserId());
                  Debug.WriteLine("USER" + user.Id);
                  announcment.TimeStamp = DateTime.Now;
                  announcment.Author = user;
                  //
                  db.Announcments.Add(announcment);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              return View(announcment);
          }
  </code></pre>
  
  <p>and it works without any trouble. No errors.</p>
  
  <p>But then I want to edit it. The view for editing looks like this. It is standard <code>Edit</code> action generated by Visual Studio 2013. I just removed editor for <code>@model.TimeStamp</code>.</p>
  
  <pre><code>@model WebApplication2.Models.Announcment
  
  @{
      ViewBag.Title = "Edit";
  }
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Announcment&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          @Html.HiddenFor(model =&gt; model.Id)
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Subject, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Subject, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Subject, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Text, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Text, new { htmlAttributes = new { @class = "form-control", rows =  8} })
                  @Html.ValidationMessageFor(model =&gt; model.Text, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>and then POST <code>Edit</code> method runs. Only thing I did here is deleting binding to <code>,TimeStamp</code> because I do not edit it. I got same results with or without it.</p>
  
  <pre><code> [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit([Bind(Include = "Id,Subject,Text")] Announcment announcment) {
              if (ModelState.IsValid) {
                  db.Entry(announcment).State = EntityState.Modified;
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              return View(announcment);
          }
  </code></pre>
  
  <p>and I get this evil error:</p>
  
  <pre><code>Błąd serwera w aplikacji '/'.
  
  The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
  The statement has been terminated. 
    Opis: Podczas wykonywania bieżącego żądania sieci Web wystąpił nieobsługiwany wyjątek. Aby uzyskać dodatkowe informacje o błędzie i miejscu jego występowania w kodzie, przejrzyj ślad stosu. 
  
   Szczegóły wyjątku: System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
  The statement has been terminated.
  
  Błąd źródła: 
  
  
  
  Wiersz 86:                 Debug.WriteLine(announcment.TimeStamp);
  Wiersz 87:                 db.Entry(announcment).State = EntityState.Modified;
  Wiersz 88:                 db.SaveChanges();
  Wiersz 89:                 return RedirectToAction("Index");
  Wiersz 90:             }
  
  
   Plik źródłowy:  c:\Users\R\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\Controllers\AnnouncmentsController.cs    Wiersz:  88 
  
  Ślad stosu: 
  
  
  
  [SqlException (0x80131904): The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
  The statement has been terminated.]
     System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1789294
     System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340642
     System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
     System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady) +1691
     System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +275
     System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite, SqlDataReader ds) +1421
     System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite) +177
     System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +208
     System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163
     System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.&lt;NonQuery&gt;b__0(DbCommand t, DbCommandInterceptionContext`1 c) +36
     System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +138
     System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) +476
     System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() +177
     System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues) +1382
     System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +236
  
  [UpdateException: An error occurred while updating the entries. See the inner exception for details.]
     System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +488
     System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.&lt;Update&gt;b__2(UpdateTranslator ut) +32
     System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T noChangesResult, Func`2 updateFunction) +272
     System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() +143
     System.Data.Entity.Core.Objects.ObjectContext.&lt;SaveChangesToStore&gt;b__35() +34
     System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +448
     System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) +219
     System.Data.Entity.Core.Objects.&lt;&gt;c__DisplayClass2a.&lt;SaveChangesInternal&gt;b__27() +71
     System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +211
     System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) +408
     System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) +39
     System.Data.Entity.Internal.InternalContext.SaveChanges() +320
  
  [DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
     System.Data.Entity.Internal.InternalContext.SaveChanges() +366
     System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +56
     System.Data.Entity.DbContext.SaveChanges() +52
     WebApplication2.Controllers.AnnouncmentsController.Edit(Announcment announcment) in c:\Users\R\Documents\Visual Studio 2013\Projects\WebApplication2\WebApplication2\Controllers\AnnouncmentsController.cs:88
     lambda_method(Closure , ControllerBase , Object[] ) +181
     System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +434
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
     System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
     System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
     System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
     System.Web.Mvc.Async.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f() +117
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass48.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__41() +323
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass33.&lt;BeginInvokeActionMethodWithFilters&gt;b__32(IAsyncResult asyncResult) +44
     System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2b.&lt;BeginInvokeAction&gt;b__1c() +72
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult) +185
     System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
     System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
     System.Web.Mvc.Controller.&lt;BeginExecute&gt;b__15(IAsyncResult asyncResult, Controller controller) +39
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
     System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
     System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
     System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9690172
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  
  
  
  
  Informacje o wersji: Wersja Microsoft .NET Framework:4.0.30319; Wersja ASP.NET:4.0.30319.34009  
  </code></pre>
  
  <p><strong>Question:</strong> Why do I get this exception when I don't even try to edit property <code>TimeStamp</code> for the <code>Announcment</code>? How to fix this?</p>
  
  <p>EDIT: I found that even when I do not try to edit original date it is mysteriously set to:
   <code>0001-01-01 00:00:00</code></p>