title: An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException'
body: <p>I´m new to mvc and I try to to do a simple Ajax call to my controller, so I can use a date- and timepickers in my create view.</p>
  
  <p>You can see this <a href="https://stackoverflow.com/questions/32714653/making-a-simple-ajax-call-to-controller-in-mvc/32715429#32715429">thread</a> where I previously asked for help. But now I have encountered a new problem.</p>
  
  <p>I get this error when I try to save my data:
  <em>An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code</em></p>
  
  <pre><code> public ActionResult CreateEvent(string createSammantrade, string createStartTime, string createEndTime, string createLokal)
      {
          Sammantrade sammantrade = new Sammantrade();
  
  
          sammantrade.sSammantrade = createSammantrade;
          sammantrade.sStartTid = Convert.ToDateTime(createStartTime);
          sammantrade.sSlutTid = Convert.ToDateTime(createEndTime);
          sammantrade.lID = Int32.Parse(createLokal);
  
          if (ModelState.IsValid)
          {
              db.Sammantrade.Add(sammantrade);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          ViewBag.lID = new SelectList(db.Lokal, "lID", "lLokal", sammantrade.lID);
          return View(sammantrade);
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre class="lang-none prettyprint-override"><code>System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
  HResult=-2146233087
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
  vid System.Data.Entity.Internal.InternalContext.SaveChanges()
  vid System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
  vid System.Data.Entity.DbContext.SaveChanges()
  vid VLVision.Controllers.SammantradesAdminController.CreateEvent(String createSammantrade, String createStartTime, String createEndTime, String createLokal) 
  vid lambda_method(Closure , ControllerBase , Object[] )
  vid System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
  vid System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  vid System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  vid System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;
  b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
  vid System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
  vid System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
  vid System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
  vid System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;
  b__3d()
  vid System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;
  b__3f()
  InnerException:
  HResult=-2146233087
  Message=An error occurred while updating the entries. See the inner exception for details.
  Source=EntityFramework
  StackTrace:
  vid System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
  vid System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.&lt;Update&gt;
  b__2(UpdateTranslator ut)
  vid System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
  vid System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
  vid System.Data.Entity.Core.Objects.ObjectContext.&lt;SaveChangesToStore&gt;
  b__35()
  vid System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
  vid System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
  vid System.Data.Entity.Core.Objects.ObjectContext.&lt;&gt;c__DisplayClass2a.&lt;SaveChangesInternal&gt;
  b__27()
  vid System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
  vid System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
  vid System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
  vid System.Data.Entity.Internal.InternalContext.SaveChanges()
  InnerException:
  Class=16
  ErrorCode=-2146232060
  HResult=-2146232060
  LineNumber=1
  Message=The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Sammantrade_Lokal". The conflict occurred in database "VLVision", table "dbo.Lokal", column 'lID'.
  The statement has been terminated.
  Number=547
  Procedure=""
  Server=localhost\SQLExpress
  Source=.Net SqlClient Data Provider
  State=0
  StackTrace:
  vid System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  vid System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
  vid System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
  vid System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)
  vid System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
  vid System.Data.SqlClient.SqlDataReader.get_MetaData()
  vid System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
  vid System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
  vid System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite)
  vid System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
  vid System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
  vid System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
  vid System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
  vid System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.&lt;Reader&gt;b__c(DbCommand t, DbCommandInterceptionContext`1 c)
  vid System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
  vid System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
  vid System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
  vid System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
  vid System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
  vid System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
  InnerException: 
  </code></pre>