如何解决DbUpdateConcurrency异常

时间:2019-04-25 13:44:44

标签: c# model-view-controller

尝试通过前端创建新记录未正确保存到数据库

因此,我从自定义代码恢复为创建页面时自动生成的代码,主要要点是我希望SSColID值在创建时为null。调试时,tableMap中的obj值正确(SSColID也为null)。我一直遇到DbUpdateConcurrency异常。 (数据库表(SSMaps)已建立,所有字段都允许NULL。如果还有其他需要,请告诉我。

// This is the snippet in the beginning of control for the db 
private APP_configs db = new APP_configs();

// the object inside the Config class being used
public System.Data.Entity.DbSet<APP_admin_v2.Models.SmartsheetMap> SmartsheetMaps { get; set; }


    // Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "KeyID,SheetName,SheetID,SSCol,VPCol,VPTable,SSColID")] SSMap tableMap)
    {
        if (ModelState.IsValid)
        {
            db.SmartsheetMaps.Add(tableMap);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(tableMap);
    }

    // Model Class
    [Table("SSMaps")]
    public partial class SSMap
    {
        [StringLength(255, MinimumLength = 1, ErrorMessage = "Required Field - Sheet Name")]
        [Display(Name = "Sheet Name")]
        public string SheetName { get; set; }

        [StringLength(255, MinimumLength = 1, ErrorMessage = "Required Field - Sheet ID")]
        [Display(Name = "Sheet ID")]
        public string SheetID { get; set; }

        [StringLength(255, MinimumLength = 1, ErrorMessage = "Required Field - SS Column Name must match exactly and is Case/Space Sensitive")]
        [Display(Name = "Smartsheet Column Name")]
        public string SSCol { get; set; }

        [StringLength(255, MinimumLength = 1, ErrorMessage = "Required Field - VP Column Name must match exactly and is Case/Space Sensitive")]
        [Display(Name = "Viewpoint Column Name")]
        public string VPCol { get; set; }

        [StringLength(255, MinimumLength = 1, ErrorMessage = "Required Field - VP Table Name must match exactly and is Case/Space Sensitive")]
        [Display(Name = "Viewpoint Table Name")]
        public string VPTable { get; set; }

        [Display(Name = "SS Column ID")]
        public string SSColID { get; set; }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long KeyID { get; set; }
    }

    // Snippet from create page (wanting the initial creation value to be null
    <div class="form-group">
        @Html.LabelFor(model => model.SSColID, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.SSColID)
        </div>
    </div>

// Additional Error Code Throw

[OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.]
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source) +5628032
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +334
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut) +11
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T noChangesResult, Func`2 updateFunction) +132
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update() +106
   System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35() +13
   System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288
   System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction) +157
   System.Data.Entity.Core.Objects.<>c__DisplayClass2a.<SaveChangesInternal>b__27() +25
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +162
   System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction) +221
   System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) +11
   System.Data.Entity.Internal.InternalContext.SaveChanges() +113

[DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.]
   System.Data.Entity.Internal.InternalContext.SaveChanges() +191
   System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +27
   System.Data.Entity.DbContext.SaveChanges() +22
   <APP>.Controllers.SmartsheetMapController.Create(SmartsheetMap smartsheetMap) in C:\Users\ad\Desktop\<APP>\<APP>_admin_v2\Controllers\SmartsheetMapsController.cs:85
   lambda_method(Closure , ControllerBase , Object[] ) +103
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__11_0() +50
   System.Web.Mvc.Async.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
   System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__3() +35
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9874041
   System.Web.<>c__DisplayClass285_0.<ExecuteStepImpl>b__0() +26
   System.Web.StepInvoker.Invoke(Action executionStep) +101
   System.Web.<>c__DisplayClass4_0.<Invoke>b__0() +22
   Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule.OnExecuteRequestStep(HttpContextBase context, Action step) +64
   System.Web.<>c__DisplayClass284_0.<OnExecuteRequestStep>b__0(Action nextStepAction) +54
   System.Web.StepInvoker.Invoke(Action executionStep) +85
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +9980101
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

0 个答案:

没有答案