Traversing an object Tree to the end in EF4

added by ananto
7/17/2012 8:23:09 AM

2 Kicks, 206 Views

Traverse the full tree of the object through their relationship/navigation until it will reach to leaf.


5 comments

dpeterson
7/17/2012 8:28:59 AM
Maybe I'm not understanding the post, but doesn't it seem silly to go to such lengths to return a list of orders for a given customer? I mean, it looks like your code using recursion, reflection, and generic invocation...is there some reason you couldn't have done this? http://msdn.microsoft.com/en-us/library/bb896249.aspx
If I've missed something important, I apologize, but your post doesn't seem to really explain why you had to go to such great lengths.

ananto
7/17/2012 8:37:15 AM
yes I would like to go into such great length because I want to backup the object Graph
into a backup file and later on it will be attached to database that might be the same database or to another database one with same schema.It will not override existing one if that any data of an object already exist , if not then it will insert the object will related object.

dpeterson
7/17/2012 8:46:57 AM
Thanks for getting back to me. So if you just need to serialize the objects, are the built-in serialization methods not sufficient if you've loaded all related entities using the method in the link I put in my last comment? Here's the MS doc on binary serialization with EF for example: http://msdn.microsoft.com/en-us/library/bb738528.aspx
Could that method work in this scenario if you had the object graph loaded by disabling lazy loading?

(PS: Sorry for not directly replying to your last comment, the comment system is giving me difficulty this morning)

ananto
7/17/2012 10:30:06 AM
Thanks for the link and great suggestion. But I am not sure , some days ago I read something :
Because POCO entities do not have the same relationship requirements as objects that inherit from EntityObject, a slightly different process is required to load related objects. For general information about loading related objects, see Loading Related Objects and Loading Related Objects.

http://msdn.microsoft.com/en-us/library/dd456855.aspx

dpeterson
7/17/2012 10:33:08 AM
Ah yes, it seems according to http://msdn.microsoft.com/en-us/library/dd468074.aspx you could accomplish this for POCO objects with just a couple linq queries to load the order entities, then use the loadproperty method to associate those entities with the correct customers.

ananto
7/17/2012 10:41:56 AM
yes but I want to traverse to the end to load all object since I want to add this object to another database[with same schema] and give it same relationship as it currently in its own database.

dpeterson
7/17/2012 10:46:28 AM
Whatever data that relates them, whatever the foreign key is, it would exist regardless of how the data is retrieved (entity framework, datareader, linq to sql, some other ORM). Once you serialize it, you can deserialize it into any other database and those relationships will still exist.

ananto
7/17/2012 10:50:09 AM
yah I know and why i am doing so. I use recursive to avoid the LINQ since I don't know how much depth of it will be .

dpeterson
7/17/2012 10:54:39 AM
Oh, I assumed from your blog post that the maximum depth was only down to the order details level.

ananto
7/17/2012 10:56:40 AM
oh ..ok I think I need to clarify that ..it may misguide others to undestand. Thanks for the comment :)