EntityGraph to copy objects in Parent-child (self referenced) table

Jan 1, 2012 at 5:17 AM

I have a need to copy the child and all related child and associated objects. The copy then has to be attached to the same parent. what is the best way to achieve this using the entitygraph. I have tried including the .Edge<x,x>.(i=>i.Parent). this results in the copy of the parent also. which is undesired.

Developer
Jan 1, 2012 at 7:02 PM
Edited Jan 1, 2012 at 7:03 PM

You need to define a graph shape that starts from your child entity and includes edges for every related entity that needs to be copied. E.g.,

var shape = new EntityGraphShape()
   .Edge<Child, SubChild1>(x => x.SubChild1)
   .Edge<Child, SubChild2>(x => x.SubChild2)
   .Edge<SubChild1, SubSubChild>(x => x.SubSubChild);

Next, given this shape and a child entity, you instantiate an entity graph for it and create a copy of the graph:

var graph = new EntityGraph(child, shape);
var copy = graph.Copy();

Finally,add the copy to your parent entity. E.g.,

parent.Children.Add(copy.Source as Child);

If you add 'using RiaServicesContrib.DomainServices.Client;', the above code can even be simplified to:

var shape = new EntityGraphShape() ... ;
var copy = child.Copy(shape);
parent.Children.Add(copy);

 

I hope this answers your question.

Jan 1, 2012 at 8:52 PM

Thanks for the quick response.  Let me try laying this out for you.

Lets say there are 3 tables

Table 1 : Parent-child related with in itself through parentid field.

Table2: AssociatedData1

Table3: AssociatedData2

I think the entity graph works very well for table2 and table3

within table 1 I have Parent1 -> Child1(Parent2) -> Child2. child1 and child2 records are required in the copy. But not Parent1. My issue is if i create the Edge for getting Child1(Parent2) -> Child2. child1 I inadvertently get Parent1 -> Child1(Parent2) also, since they are defined by the same realtion. How do i filter out the root parent but still get the sub parent relations copied.

Thanks

 

Developer
Jan 1, 2012 at 9:28 PM

I'm sorry, I've difficulties understanding your tables (don't know for example what you mean by Child1(Parent2)). Could you therefore provide small C# classes demonstrating the issue. Could you also provide your entity graph shape that you're using.

Thanks.

Jan 2, 2012 at 4:55 AM

class Machinecomponent

{

public int ID,

public string Name,

public int ParentID,

public Machinecomponent Parent

}

class cost

{

int MachinecomponentID

decimal Cost 

}

class Supplier

{

int MachinecomponentID

int SupplierID

}

Lets say we have

ID---  Name----ParentID

1-----ParentComponent1---- -1

2-----Component1----1

3-----Component2----1

4-----ParentComponent2---- -1

5-----Component3----4

6-----Component4----4

7-----Component5----6

Consider machine component stored in a hierarchical relation in a table. A component can be a part of a bigger component. If component4 is needed in Parentcomponent1 then i would make a copy of component4 and attach it to Parentcomponent1.

the edge relation in this case would be

Edge<Machinecomponent,Machinecomponent>(i=>i.Parent).

But this would get me not only the relation between Component4 and Component5 but also ParentComponent2 since ParentComponent2 and Component4 have the same relation. It is not desired to copy ParentComponent2 so I need to filter this out of the Graph object somehow.

As I have said before EntityGraph works great to copy related table likes cost and Supplier

Hope this is more clear.

 

Jan 2, 2012 at 6:49 AM

I think i am closer to getting past this issue. I might have been in error in using i.Parent than using i.children in the entitygraph. Still working through it. I think i am closer though. any inputs would be helpful.

 

Jan 2, 2012 at 7:12 AM

After running some m ore tests I believe i have resolved my issue. thanks for your help and quick feedbacks. Thanks for the entitygraph tool. Really good piece of work. works liek a charm( if you use it right ;-)).

Developer
Jan 2, 2012 at 9:38 PM

Good to hear that your problem is solved. good luck.