Problem saving an entity from one DB to another

Posts   
 
    
bot2600
User
Posts: 17
Joined: 12-Mar-2006
# Posted on: 11-Oct-2006 15:26:27   

Good morning, I have an odd problem. I am fetching an entity from one database to be saved to another database. I am setting its IsNew property before the save, but it does not try to save the entity. I went one step further and set the IsDirty property to true, but it doesnt try to save any fields, as their changed property is null. Is there anyway to get around this? I cant just loop through the entity and manually set the fields, as I will be prefetching about 7 other tables eventually for this.

Thanks Bob

bot2600
User
Posts: 17
Joined: 12-Mar-2006
# Posted on: 11-Oct-2006 15:53:43   

Actually I just found this (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7568&HighLight=1), and it looks like it would cover the prefetch stuffs as well. I'll give it a try. Thanks!

Bob

update,

that code snippet actually doesnt compile for me, I get an error on this line

List<IEntity> flatList = ogu.ProduceTopologyOrderedList(newEntity);

Cannot implicitly convert type 'System.Collections.ArrayList' to 'System.Collections.Generic.List<SD.LLBLGen.Pro.ORMSupportClasses.IEntity>'

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 12-Oct-2006 08:58:18   

If you are using .NET 2.0 -> ProduceTopologyOrderedList returns List<IEntity> If you are using .NET 1.x -> ProduceTopologyOrderedList returns ArrayList

So make sure you are referencing the correct version of the SD.LLBLGen.Pro.ORMSupportClasses.NETxx.dll

MatthewM
User
Posts: 78
Joined: 26-Jul-2006
# Posted on: 12-Oct-2006 18:42:21   

bot2600 wrote:

Actually I just found this (http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7568&HighLight=1), and it looks like it would cover the prefetch stuffs as well. I'll give it a try. Thanks!

Oh what an honor someone referencing my code. smile (which is really just other people's code wrapped up tidily)

Anyway, do know that the cloning code there will only clone already fetched related entities, it won't do any lazy loading that isn't already done. If you knew that, please ignore.

bot2600
User
Posts: 17
Joined: 12-Mar-2006
# Posted on: 13-Oct-2006 19:47:10   

If you are using .NET 2.0 -> ProduceTopologyOrderedList returns List<IEntity> If you are using .NET 1.x -> ProduceTopologyOrderedList returns ArrayList

I am definitely using 2.0, but I was able to get it working.

@MatthewM the code you wrote is clean and nice and was exactly what I was looking for. Thank you much. I still got the same conversion error, I just changed the method somewhat.

Let me see what is different for me.

I ended up with this

public class CloneHelper
{
    private CloneHelper()
    {
    }

    internal static object CloneObject(object o)
    {
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
        bf.Serialize(ms, o);
        ms.Seek(0, SeekOrigin.Begin);
        object oOut = bf.Deserialize(ms);
        ms.Close();
        return oOut;
    }

    internal 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)
    {
        IEntity2 newEntity;

        newEntity = (IEntity2)CloneObject(Entity);
        ObjectGraphUtils ogu = new ObjectGraphUtils();
        ArrayList flatList = ogu.ProduceTopologyOrderedList(newEntity);

        for (int f = 0; f < flatList.Count; f++)
            ResetEntityAsNew((IEntity2)flatList[f]);

        return newEntity;
    }
}

}

Either way, your code got me where I needed to be and is greatly appreciated wink

Bob

krkc
User
Posts: 20
Joined: 12-Apr-2006
# Posted on: 28-Feb-2007 11:55:21   

bot2600 wrote:

If you are using .NET 2.0 -> ProduceTopologyOrderedList returns List<IEntity> If you are using .NET 1.x -> ProduceTopologyOrderedList returns ArrayList

o I am definitely using 2.0, but I was able to get it working.

@MatthewM the code you wrote is clean and nice and was exactly what I was looking for. Thank you much. I still got the same conversion error, I just changed the method smewhat.

Let me see what is different for me.

I ended up with this

public class CloneHelper
{
    private CloneHelper()
    {
    }

    internal static object CloneObject(object o)
    {
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
        bf.Serialize(ms, o);
        ms.Seek(0, SeekOrigin.Begin);
        object oOut = bf.Deserialize(ms);
        ms.Close();
        return oOut;
    }

    internal 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)
    {
        IEntity2 newEntity;

        newEntity = (IEntity2)CloneObject(Entity);
        ObjectGraphUtils ogu = new ObjectGraphUtils();
        ArrayList flatList = ogu.ProduceTopologyOrderedList(newEntity);

        for (int f = 0; f < flatList.Count; f++)
            ResetEntityAsNew((IEntity2)flatList[f]);

        return newEntity;
    }
}

}

Either way, your code got me where I needed to be and is greatly appreciated wink

Bob

Hi Bob,

How can I save the (IEntity2)flatList[f] as a new record (without updating)

Regards, Kalyan

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 28-Feb-2007 15:03:56   

The above mentioned method ResetEntityAsNew, does the trick.

krkc
User
Posts: 20
Joined: 12-Apr-2006
# Posted on: 01-Mar-2007 07:32:19   

Walaa wrote:

The above mentioned method ResetEntityAsNew, does the trick.

Hi Walaa,

I have tried using the same code in my senario, but I was still not able to save the entity as a new record. Instead, the entity is getting updated.

Here is my code for your reference.


public static class CloneHelper<T>  where T : SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2, SD.LLBLGen.Pro.ORMSupportClasses.IEntity2
{

 public static void EntityCollectionSet(EntityCollection<T> studySets)
    {
        for (int i = 0; i < studySets.Count; i++)
        {
           CloneEntity(studySets[i]);
        }
    }

   public static object CloneObject(object o)
        {
        
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
            bf.Serialize(ms, o);
            ms.Seek(0, SeekOrigin.Begin);
            object oOut = bf.Deserialize(ms);
            ms.Close();
            return oOut;
        }

    public static IEntity2 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;
            }
            return Entity;
        }

    public static void CloneEntity(IEntity2 Entity)
        {
            DataAccessAdapter adaptorToStudySet = new DataAccessAdapter();
            IEntity2 newEntity;

            newEntity = (IEntity2)CloneObject(Entity);
            ObjectGraphUtils ogu = new ObjectGraphUtils();
            List<IEntity2> flatList = ogu.ProduceTopologyOrderedList(newEntity);

            for (int f = 0; f < flatList.Count; f++)
            {
                adaptorToStudySet.SaveEntity(ResetEntityAsNew(flatList[f]));
            }
        }
    }

My requirement is that the entity should not get updated, instead a new record should be created.

Regards, Ravi

sreenadha
User
Posts: 1
Joined: 01-Mar-2007
# Posted on: 01-Mar-2007 07:57:31   

krkc wrote:

Walaa wrote:

The above mentioned method ResetEntityAsNew, does the trick.

Hi Walaa,

I have tried using the same code in my senario, but I was still not able to save the entity as a new record. Instead, the entity is getting updated.

Here is my code for your reference.


public static class CloneHelper<T>  where T : SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2, SD.LLBLGen.Pro.ORMSupportClasses.IEntity2
{

 public static void EntityCollectionSet(EntityCollection<T> studySets)
    {
        for (int i = 0; i < studySets.Count; i++)
        {
           CloneEntity(studySets[i]);
        }
    }

   public static object CloneObject(object o)
        {
        
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
            bf.Serialize(ms, o);
            ms.Seek(0, SeekOrigin.Begin);
            object oOut = bf.Deserialize(ms);
            ms.Close();
            return oOut;
        }

    public static IEntity2 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;
            }
            return Entity;
        }

    public static void CloneEntity(IEntity2 Entity)
        {
            DataAccessAdapter adaptorToStudySet = new DataAccessAdapter();
            IEntity2 newEntity;

            newEntity = (IEntity2)CloneObject(Entity);
            ObjectGraphUtils ogu = new ObjectGraphUtils();
            List<IEntity2> flatList = ogu.ProduceTopologyOrderedList(newEntity);

            for (int f = 0; f < flatList.Count; f++)
            {
                adaptorToStudySet.SaveEntity(ResetEntityAsNew(flatList[f]));
            }
        }
    }

My requirement is that the entity should not get updated, instead a new record should be created.

Regards, Ravi

Dear walaa,

I am also facing same problem with this, I have sevaral relational tables , i want to copy some of the related table rows as new with new autogenarated Ids , I don't want to disturb Old records .

Regards Sreenadhaconfused

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Mar-2007 08:47:34   

First you can examine the generated SQL statements. Ref. LLBLGen Pro manual "Using the generated code -> Troubleshooting and debugging"

Are you using Identity PKs?

Which LLBLGenPro runtimeLibrary version are you suing?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 01-Mar-2007 11:00:36   

If IsNew is true, an insert query is generated, always. So be sure IsNew is true for the entities you're saving.

Frans Bouma | Lead developer LLBLGen Pro