Using a single entity will not reproduce the problem. I am not having any issues with the prefetch or the clone. It am running into an issue when cloning a child table using a m:n relationship. I have attached an image of a simplified example of this... It contains four tables..
From the image, I am attempting to clone the project. I can clone everything as expected except for the InspSchedPermitAssoc Table. With the current prefetch the InspSchedPermitAssoc table grabs a new ProjectPermitAssocID (this is good), but is using the InspSchedID from the original Project Entity and not from the newly created one. I am regraphing through the project entity after the fact to fix this, but I just need to know if there is something i can do within the prefetch to make sure that the clone will get the newly created InspSched Ids and not reuse the one from the orginal source entity.
I also have included the simple prefetch code and cloner helper class.
btw, thx for having a look. Let me know if you need anything else.
Prefetch:
public IPrefetchPath2 GetProjectRelationsCopy()
{
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.ProjectEntity);
IPrefetchPathElement2 pfPermitElement = prefetchPath.Add(ProjectEntity.PrefetchPathProjectPermitsAssoc);
pfPermitElement.SubPath.Add(ProjectPermitsAssocEntity.PrefetchPathInspSchedPermitAssoc ).SubPath.Add(InspSchedPermitAssocEntity.PrefetchPathInspSched);
//pfPermitElement.SubPath.Add(ProjectPermitsAssocEntity.Pref)
IPrefetchPathElement2 pfScheduleElement = prefetchPath.Add(ProjectEntity.PrefetchPathInspSched);
pfScheduleElement.SubPath.Add(InspSchedEntity.PrefetchPathInspSchedPermitAssoc);
return prefetchPath;
}
Clone Helper
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace GovPartner.CDP.CDPAdapterServices.Helper
{
/// <summary>
/// For cloning an Entity and all related entities, ie the whole graph
/// </summary>
public class CloneHelper
{
public CloneHelper()
{
}
private static object CloneObject(object o)
{
object oOut;
lock (o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
bf.Serialize(ms, o);
ms.Seek(0, SeekOrigin.Begin);
oOut = bf.Deserialize(ms);
ms.Close();
}
return oOut;
}
private static void ResetEntityAsNew(IEntity2 Entity)
{
Entity.IsNew = true;
Entity.IsDirty = true;
Entity.Fields.IsDirty = true;
for (int f = 0; f < Entity.Fields.Count; f++)
{
Entity.Fields[f].IsChanged = true;
}
}
public static IEntity2 CloneEntity(IEntity2 Entity, bool ResetAsNew)
{
IEntity2 newEntity;
newEntity = (IEntity2)CloneObject(Entity);
ObjectGraphUtils ogu = new ObjectGraphUtils();
List<IEntity2> flatList = ogu.ProduceTopologyOrderedList(newEntity);
if (ResetAsNew)
for (int f = 0; f < flatList.Count; f++)
ResetEntityAsNew(flatList[f]);
return newEntity;
}
public static IEntity2 CloneEntity(IEntity2 Entity)
{
return CloneEntity(Entity, true);
}
public static T CloneEntity2<T>(T Entity)
where T : EntityBase2
{
return (T)CloneEntity(Entity, true);
}
public static T CloneEntity2<T>(T Entity, bool ResetAsNew)
where T : EntityBase2
{
return (T)CloneEntity(Entity, ResetAsNew);
}
}
}