Question

I'm getting the following error: "deleted object would be re-saved by cascade (remove deleted object from associations)"

I have trimmed the entirety of the ajax call to the following:

    [HttpPost]
    [UnitOfWork(Scope = FilterScope.Result)]
    public ActionResult SaveEditMode(long id, AddTrackedRowViewModel model, string editMode, List<string> elementNames, string provisionData)
    {    
       var cell = _supplementCoordinator.GetSupplement(id).TrackedTables.First(x => x.Name == model.Name).TrackedRows.First(x => x.Ordinal == model.Ordinal).TrackedCells.First(x => x.Name == "Detail");
       _supplementCoordinator.RemoveChildren(cell);

        return Json( new {Success = true});
    }



    public bool RemoveChildren(TrackedNode parentNode)
    {
        foreach (TrackedField trackedField in parentNode.ChildNodes)
        {
            _trackedFieldRepository.Delete(trackedField);
        }
        return true;
    }

My mappings are as follows

        mapping.HasMany(x => x.ChildNodes).KeyColumn("ParentNodeId").Inverse();
        mapping.References(x => x.ParentNode);
Was it helpful?

Solution

Just remove the child nodes from the parent collection just as the error suggests:

public bool RemoveChildren(TrackedNode parentNode)
    {
        foreach (TrackedField trackedField in new List<TrackField>(parentNode.ChildNodes))
        {
            _trackedFieldRepository.Delete(trackedField);
            _parentNode.Remove(trackField);
        }
        return true;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top