Copy of an entity with all the relations

Posts   
 
    
dorit
User
Posts: 16
Joined: 03-Jul-2006
# Posted on: 01-Oct-2006 10:30:23   

Hi,

I was trying to copy an Entity to another entity with all its relations but the copy entitied is a copy that points to the original so changes on it affect the original. My question is how can I copy an Entity with all its relations (incluede one to many and one to many relatiosn) , a real copy that changes on it won't affect the original9doesn't point to it). The question seems very trivial but I tried some approaches and didn't work.First I tried to use the constructor with the Fields paramater and make a copy ,of course that the relations where not copied. Then I tried to copy the Entitiy to an EntityCollection, add the Original entity to the collection and then copy it with to another entity, but this also leads that changes done on copy affect the original even if I use the CopyTo function of the collection. Thanks in advance

dorit
User
Posts: 16
Joined: 03-Jul-2006
# Posted on: 01-Oct-2006 11:18:25   

Does the answer is to use The memory stream trick as explain in http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=1834&HighLight=1. Is another way? Thanks

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 01-Oct-2006 18:26:11   

Please see the guidelines for forum posts:

http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7717

We need more information to help with your issue. Thanks.

dorit
User
Posts: 16
Joined: 03-Jul-2006
# Posted on: 03-Oct-2006 14:51:10   

Hi, Regarding the info requested:

LLBLGen Pro version + buildnr : Version 2.0.0.0 Final DEMO Released on August 3rd 2006

it's a problem occuring at runtime, File Version 2.0.0.60803

template group + .NET version you're using Adapter .net 2.0

database type and version you're using : SqlServer 2005

real code:

Suppose you have an orderEntity that has all relations.

EntityCollection<OrdEntity> orders = new EntityCollection<OrdEntity>(new OrdEntityFactoreToUse())

orders.Add(orderEntity);

OrdEntity[] orderArray = new OrdEntity[1]; orders.CopyTo(orderArray,0);

//The problem is that i make the folloiwng:

orderArray.cutomer = 1; at runtime the orderEntity changes it customer value to 1 also.

hope it helps Thanks

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 03-Oct-2006 16:17:56   

Serialization should help with your copy.

Either Binary (faster) or XML (easier) serialization are fine. They include all the related entities.

Without using a serializer, you could even simply do:

yourOrginalMainEntity.WriteXML(yourXMLString) yourCopiedMainEntity.ReadXML(yourXMLString)

Cheers

jaschag
User
Posts: 79
Joined: 19-Apr-2006
# Posted on: 04-Oct-2006 13:36:36   

dorit wrote:

Hi, Regarding the info requested:

LLBLGen Pro version + buildnr : Version 2.0.0.0 Final DEMO Released on August 3rd 2006

it's a problem occuring at runtime, File Version 2.0.0.60803

template group + .NET version you're using Adapter .net 2.0

database type and version you're using : SqlServer 2005

real code:

Suppose you have an orderEntity that has all relations.

EntityCollection<OrdEntity> orders = new EntityCollection<OrdEntity>(new OrdEntityFactoreToUse())

orders.Add(orderEntity);

OrdEntity[] orderArray = new OrdEntity[1]; orders.CopyTo(orderArray,0);

//The problem is that i make the folloiwng:

orderArray.cutomer = 1; at runtime the orderEntity changes it customer value to 1 also.

hope it helps Thanks

I think this thread contains your answer - you need to clone not just copy references - http://llblgen.com/TinyForum/Messages.aspx?ThreadID=7568&HighLight=1

CloneObject will create an deep copy of your object and its graph, CloneEntity will do the same as CloneObject but also set all cloned entities to be new so they will be saved as distinct entites to those that they were cloned from. HTH