New Posts New Posts RSS Feed: Deep copy an entity tree
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Deep copy an entity tree

 Post Reply Post Reply
Author
bigme View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2010
Location: Australia
Posts: 14
Post Options Post Options   Quote bigme Quote  Post ReplyReply Direct Link To This Post Topic: Deep copy an entity tree
    Posted: 24-Aug-2010 at 4:44pm
I have an existing Order and a collection of OrderDetails that I need to duplicate as the basis for a new order. At the moment I simply create new entities and copy the properties I want. Is there a less brittle, more DevForce-oriented way to do this?

Dave.

Back to Top
bigme View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2010
Location: Australia
Posts: 14
Post Options Post Options   Quote bigme Quote  Post ReplyReply Direct Link To This Post Posted: 25-Aug-2010 at 3:29pm
The Order and OrderDetails are, of course, standard IdeaBlade-generated objects and collections, a la Northwind.

Dave.
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 25-Aug-2010 at 7:43pm
You can clone an entity by explicitly casting to IClonable and calling Clone().  This will return you a detatched entity with all of the fields copied.  You should now call EntityManager.GenerateID() to assign it a new primary key and add it to the EntityManager.  Make sure to assign the primary key before adding it to the EntityManager.
 
Here's an example:
Employee newEmployee = (Employee) ((ICloneable)employee).Clone();
mgr.GenerateId(newEmployee, Employee.PropertyMetadata.EmployeeID);
mgr.AddEntity(newEmployee);
 
Back to Top
bigme View Drop Down
Newbie
Newbie
Avatar

Joined: 13-Aug-2010
Location: Australia
Posts: 14
Post Options Post Options   Quote bigme Quote  Post ReplyReply Direct Link To This Post Posted: 25-Aug-2010 at 9:20pm
Thanks Ting,

 If I do as you say above then I get a new order (without OrderDetails), but the original order has its PK changed to the same value as the newly added order !!

So it seemed that I should NOT explicitly add the cloned Order to the mgr.Orders collection, but just rely on the cloning of the Customer navigation property? When I do this I don't get a new Order, but it still changes the PK of the current order (the one being cloned)

I suspect this is related to the fact that I am working with a collection of customer.Orders and not mgr.Orders??

Dave.
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 26-Aug-2010 at 4:28pm

My bad.  I should have tested this first.  ICloneable.Clone does not have the correct implemenation.  I have an architect looking at this.



Edited by ting - 26-Aug-2010 at 4:29pm
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 31-Aug-2010 at 1:37pm
Clone will be fixed for the release this week.  Sorry for the false start!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down