Entity Framework - Delete Circular References RRS feed

  • Question

  • Dear community,

    I am struggling since a few days with a problem when deleting entities that contain several circular references. I have a model that describes a hierarchy of objects that contain references to their parent entities. Now I want to delete the root entity of such a hierarchy. My first approach was to let all entities cascade on delete but this does not work out since the cascade path contains circular references and EF doesn’t like that.

    So I tried to do it manually. This somehow works out but I have to load every child entity and call remove on it. This causes a bunch of roundtrips to the database and slows down the performance a lot. Also every model change requires a change in the delete code. The manual way might be a solution but I doubt it is the best way to get the problem solved.

    Are there any best practices to handle such a scenario? I would be very happy if anybody could point me to another approach or source where I can find new ideas.

    Thanks a lot in advance,


    Wednesday, July 11, 2012 4:40 PM


  • Hi Matthias;

    If you are defining cascade on delete in the model only you will also need to define it in the database as the EF cascade on delete only deletes in local memory child entities.

    If you are getting some kind of an error please post it here.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, July 11, 2012 6:29 PM