Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breeze SaveChanges always throws DbUpdateConcurrencyException when deleting entity

I have just enabled the "Concurrency Mode" property to fixed of one of my entity.

Everything works great when I try to update.

But when I try to delete the entity, I always get this error :

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.

Is there any way to disable DBUpdateConcurrencyException for delete operation? If not, how can I manage this type of exception?

[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{

    try
    {
        return _breezeComponent.SaveChanges(saveBundle);
    }
    catch (DbUpdateConcurrencyException ex)
    {               
        //Workaround needed
    }

}

BTW, I have already looked at these kinds of solution : How to ignore a DbUpdateConcurrencyException when deleting an entity . Is there any way I can integrate this code with Breeze engine?

EDIT: I have upgraded from version 1.4.5 to 1.4.7 and I still have the same problem.

If I look at the JSON object, would changing the entityState from "Deleted" to "Detached" would be a solution? Is there any setting in Breeze that can help me do that?

{
  "entities": [
    {
      "EventId": 11111,
      "EventName": "Jon Doe",
      "EventCity": "Montreal",
      "EventDate": "2014-01-24T00:00:00Z",
      "TermDate": "2014-01-08T00:00:00Z",
      "Insertedby": "Terry",
      "InsertDate": "2014-01-06T14:31:14.197Z",
      "Updatedby": "Terry",
      "UpdateDate": "2014-01-07T15:50:53.037Z",
      "entityAspect": {
        "entityTypeName": "Event:#Cds.Corpo.GuestList.Models",
        "defaultResourceName": "Events",
        "entityState": "Deleted",
        "originalValuesMap": {},
        "autoGeneratedKey": {
          "propertyName": "EventId",
          "autoGeneratedKeyType": "Identity"
        }
      }
    }
  ],
  "saveOptions": {}
}
like image 232
Elferone Avatar asked Oct 20 '22 17:10

Elferone


2 Answers

Just a guess, but do you have some form of cascaded delete turned on in your database. If so, then the issue may be that the delete of a parent is causing the child to also be deleted and when the breeze server code kicks in it tries to delete the 'already' deleted child again. When this delete cannot find the child it throws the concurrency exception.

The way to avoid this is to use the BeforeSaveEntities interception point and remove all of the children of any deleted entities ( that are part of the cascade delete relationship) from the 'saveMap'. See the Breeze documentation on BeforeSaveEntities on this page: http://www.breezejs.com/documentation/contextprovider

like image 90
Jay Traband Avatar answered Oct 29 '22 23:10

Jay Traband


There was a mecanism hidden in our application framework that did update the property before saving changes to the database. So this wasn't a Breeze issue.

A filter has now been added to the solution to exclude this pattern when deleting.

like image 32
Elferone Avatar answered Oct 29 '22 23:10

Elferone