0
votes

I'm getting this error:

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

dbContext.Productos.Add(producto);
dbContext.Entry(producto).State = System.Data.Entity.EntityState.Modified;
dbContext.SaveChanges();
 }

Any idea about what is wrong here?

Thanks.

My stack trace is:

[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. Refresh ObjectStateManager entries.]
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source) +232
   System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() +493
   System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>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.<SaveChangesToStore>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.<>c__DisplayClass2a.<SaveChangesInternal>b__27() +71
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +213
   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

[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. Refresh ObjectStateManager entries.]
   System.Data.Entity.Internal.InternalContext.SaveChanges() +366
   System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +56
   System.Data.Entity.DbContext.SaveChanges() +52
   Clase5.Data.ProductoRepositorio.Insertar(Producto producto) in c:\Users\Juan\Downloads\Clase_5_-_Codigo_fuente\Clase5\Clase4\Data\ProductoRepositorio.cs:27
   Clase5.Controllers.ProductoController.TresCrearProducto(Producto producto) in c:\Users\Juan\Downloads\Clase_5_-_Codigo_fuente\Clase5\Clase4\Controllers\ProductoController.cs:43
   lambda_method(Closure , ControllerBase , Object[] ) +180
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +211
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9657896
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
2
remove this line: dbContext.Entry(producto).State = System.Data.Entity.EntityState.Modified; - codebased
@codebased Still the same error.. - Juan Curti
Need to do see if it is really happening for producto or you are trying to save more than one object? - codebased
The first line you did Productos.Add(producto) the second line you did Entry(producto).State = EntityState.Modified, you wanted to add a new entity or modify existing entity, which is it? - Yuliam Chandra
@codebased Here is the link to download all the files..can you see if you can get the error, please? I've read every single line and can't deal with it.. mega.co.nz/… - Juan Curti

2 Answers

0
votes

You have implemented a field level concurrency in Producto entity. Go to the EDMX designer of your Producto entity. Go through properties of each column one by one. Select the column in the visual entity designer -> Press F4. Check the value of the property Concurrency Mode. It should be set to None. Have a look at below snapshot as sample for one of my entities Employee. If for any of the columns, you see the value as Fixed then that's the root cause of your error.

If you are really thinking of doing field level concurrency then use RowVersion instead.

enter image description here

0
votes

Have you set AutoDetectChangesEnabled to false? If you have done that,please call DetectChanges manually before SaveChanges, don't change the Entry State directly.