Base solution for your next web application
Open Closed

Delete Parent Child Data in ABP #493


User avatar
0
potatoboro created

Hi,

I got "The DELETE statement conflicted with the REFERENCE constraint...." error when using Unit of Work Delete(TPrimaryKey id) function for my parent child data/table. The id that I passed in into the delete function is the parent id.

I understand that this is due to I tried to delete the parent without deleting the children. What is the correct way to do this?

I had also tried to use Delete(TEntity entity) and supply the entity with the parent object (children included), but still no luck.

var parentObj= _parentRepository.Get(parentId);
_parentRepository.Delete(parentObj);

2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This is not a unique problem to ABP. If you don't use ABP and use plain EntityFramework, same will happen. So, what's the solution? There are some alternatives:

    1. You can define cascade delete in db level.
    2. You can delete children first.
    3. You can make the parent ISoftDelete.

    All has advantages and disadvantages. There maybe other alternatives, these are what I'm making for such cases based on the case.

  • User Avatar
    0
    potatoboro created

    <cite>hikalkan: </cite> Hi,

    This is not a unique problem to ABP. If you don't use ABP and use plain EntityFramework, same will happen. So, what's the solution? There are some alternatives:

    1. You can define cascade delete in db level.
    2. You can delete children first.
    3. You can make the parent ISoftDelete.

    All has advantages and disadvantages. There maybe other alternatives, these are what I'm making for such cases based on the case.

    Hi Hikalkan,

    Thanks for your reply. Yeah i guess i will try to sort it out from EF level.

    Thank You.