Print Page | Close Window

Cloning an Entity

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=731
Printed Date: 11-Jun-2026 at 6:22am


Topic: Cloning an Entity
Posted By: orcities
Subject: Cloning an Entity
Date Posted: 18-Mar-2008 at 10:47am
When cloning an entity it only clones the base entity. When I try and attach to a referenced object it still does not attach. Why is this.
 
Example:
 
Contact aClone = aContact.Clone();
aClone.Address = SomeAddress;
 
Outcome:
aClone.Address still has values that are blank.
 
 



Replies:
Posted By: pkarsh
Date Posted: 18-Mar-2008 at 3:33pm
Could you show us your cloning code? Also, how is the relation between Contact and Address defined (i. e. 1-to-1 or 1-to-many and who is the parent and who is the child?)


Posted By: orcities
Date Posted: 19-Mar-2008 at 8:05am
Originally posted by orcities

 
Example:
 
Contact aClone = aContact.Clone();
aClone.Address = SomeAddress;
 
I have also tried attaching a PM to the clone:
Contact aClone = aContact.Clone(MainPM);
 
 


Posted By: orcities
Date Posted: 19-Mar-2008 at 8:07am
Contact-Phone is 1-to-many but I have examples that are 1-to-1 that do the same thing.


Posted By: pkarsh
Date Posted: 19-Mar-2008 at 10:14am
I meant the code for your Clone method. The Entity class does not have a Clone method so you must have added your own Clone method. Also, in your example, which Entity is the parent and which is the child?


Posted By: orcities
Date Posted: 19-Mar-2008 at 10:22am

I am using the Cabana framework.

/// <summary>Create clone of this entity; it is associated with but NOT attached to a PM</summary>

/// <remarks>

/// The clone gets its <see cref="PersistenceManager"/> from this entity.

/// See <see cref="Clone(PersistenceManager)"/>.

/// </remarks>

public virtual Entity Clone() {

return Clone(PersistenceManager);

}

 

public virtual Entity Clone(PersistenceManager pManager) {

Entity aClone = pManager.CreateEntity(GetType());

      aClone.ItemArray = ItemArray;

      return aClone;

}

 
I would think that overriding the associated object would be suffice. But the reference doesn't take.


Posted By: pkarsh
Date Posted: 20-Mar-2008 at 11:27am
The short answer is that in order for the Entity you have created by cloning (aClone in your example) to recognize relational entity objects referenced by dot syntax (e. g. aClone.Address), you have to call AddToManager() on the newly-generated entity. In your example, before you do the AddToManager you will have to do something to make the primary key of aClone unique. As it stands, aClone will have the same primary key value as the entity it was cloned from and the AddToManager call will fail.



Print Page | Close Window