Delete entity with its associated entityies

Aug 16, 2011 at 7:05 AM

Dear RIA Services Contrib team,

First thanks for a great tool.

We are trying to test the delete function and wishes to get some guidance. For example, in the car park example, we would like to delete a truck with all its engines & doors deleted ( even the sub-entities are not loaded into the memory yet ).

We did not see any deleting code snip in the car park sample. Could you add some instructions over this topic? Thanks very much.

 

P.S. didn't find the source code for the car park sample. did we miss something?

Best Wishes

Max

Developer
Aug 18, 2011 at 1:07 PM

Hi,

Thanks for trying EntityGraph.

Deletion is tricky if not all data is in your context. But thanks to EntityGraph and cascading deletes it can be done without a lot of effort.
I would implement deletion as follows:

  1. In the entity model, define a graph of entities by marking associations as cascading. This will instruct EF to delete all child objects if a parent is deleted.
  2. Next, define an entity graph shape that follows the graph you just defined in the entity model. For example:
    var shape = new EntityGraphShape()
        .Edge<Car,Engine>(x => x.Engine)
        .Edge<Car,Wheel>(x => x.Wheels)
        .Edge<Car,Door>(x => x.Doors)
        .Edge<Car, Owner>(x => x.Owner);
    
  3. Now, if you want to delete an instance of Car in your Silverlight client, do the following:
    var graph = car.EntityGraph(shape);
    graph.DetachEntityGraph(mycontext.Cars);
    var carClone = car.Clone(new EntityGraphShape());
    mycontext.Cars.Attach(carClone);
    mycontext.Cars.Remove(carClone);
    mycontext.SubmitChanges(); 

In step 3 the following is happening. First, you instantiate an entity graph given the provided shape. Then you detach all entities of this graph from the context. They are now no longer managed by the context. Then you create a clone of the car that you want to delete. By passing an empty shape, only the car entity is cloned (not any of its associated entities). We attach this clone to the context, and delete it. Since this clone is managed by the context, calling SubmitChanges will invoke the delete domain service operation on the server for this car. Since entity framework knows this is a cascading delete, it will also delete all child entities of the car.

The trick is to use EntityGraph to remove a car and its children from the context in your Silverlight client and cascading deletes to remove them from the data base. This way both your context and your data base remain consistent, without having to first load all data in your Silverlight client.

The source for the car park example is included in the test set of entity graph. E.g., you can have a look at it at this URL http://riaservicescontrib.codeplex.com/SourceControl/changeset/view/64646#1094078.

Good luck.

Aug 22, 2011 at 2:13 PM

Thanks very much Mdej. This post will definitely to be a good starting point.

Then I am thinking about to call the stored procedure to do all the "dirty work" on DB side when deleting (Delete all the sub entities by SQL). What do you think? Is it a good way or not? 

Developer
Aug 22, 2011 at 9:15 PM

Good. Let me know if you get it working.

It depends on your needs. I think, cascading delete is pretty much standard database functionality. Moreover, it is supported by EF. So why implementing such functionality yourself?