views:

273

answers:

2

I am creating software where user can create new product based on older product.

Now I need to make copying / cloning operations with Entity Framework. First I started writing like this:

foreach(sourcedata1 in table1)
{
   ... create new table
   ... copy data
   ... create Guid
   ... add
   foreach(sourcedata2 in table2)
   {
       ... create new table
       ... copy data
       ... create Guid
       ... add       

       ... and so on
   }
}

Problem is that this not a nice way to do it. Is there any easy way clone information (except Guid that needs to be generated for new rows) or should I manually copy everything?

+1  A: 

Using straight serialization, you can do this:

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/a967b44b-c85c-4afd-a499-f6ff604e2139/

Using Reflection, but with lots more code you can google for "Entity Cloner Matthieu Mezil" (new user single-hyperlink restriction, sorry)

kfm2000
It seems that there isn't nicer solution, so I'll accept this as an answer :)
Tx3
A: 

To clone an Entity in Entity Framework you could simply Detach the entity from the DataContext and then re-add it to the EntityCollection.

      context.Detach(entity);
      entityCollection.Add(entity);
ChrisNel52