Saving Entity to other database

Posts   
 
    
BertJ
User
Posts: 8
Joined: 12-Dec-2008
# Posted on: 23-Dec-2008 22:22:40   

Hello,

I have a question, I have two databases with exactly the same schema. I want to use LLBLGen to fetch an object (with all sub objects) using Linq and save it to the other database (where I now for sure the entity isn't already).

I tried setting IsNew and IsDirty to true (of the entity and all sub entitites) but when saving to the other database nothing happens.

I use UnitOfWork2 to save these entities and when I get the InsertQueue Or UpdateQueue these are empty.

Can you please help me?

What are the best practices / pitfalls when using LLBLGen to copy/sync entities between database servers with the same schema?

Thanks! Grts,

PS 1: - We use adapter PS 2: - I also want to keep the primary keys the same so later on I can map them, is this possible?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Dec-2008 03:57:03   

Hi Bert,

Your method should work ok. Are you setting the _IsChanged _flag of every field to true? You should set this flag, if the framework doesn't detect changed fields, no insert/update will occur.

If the problem persists please provide LLBLGen runtime library version and your code snippet.

David Elizondo | LLBLGen Support Team
BertJ
User
Posts: 8
Joined: 12-Dec-2008
# Posted on: 24-Dec-2008 10:24:23   

Thanks for the info,

after searching the forum I found a solution:

        
private static void SaveAppointment(AppointmentsEntity vAppointment)
{
            var vGraph = new ObjectGraphUtils();

            using (var adapter = new DataAccessAdapter(ConnectionString))
            {
                var vUnitOfWork = new UnitOfWork2();

                foreach (IEntity2 vE in vGraph.ProduceTopologyOrderedList(vAppointment))
                {
                    vE.IsNew = true;
                    vE.Fields.IsDirty = true;

                    for (int i = 0; i < vE.Fields.Count; i++)
                    {
                        vE.Fields[i].IsChanged = true;
                    }

                    vUnitOfWork.AddForSave(vE);
                }

                vUnitOfWork.Commit(adapter, true);
            }
         }

Now I have a different problem, I want to set the primary key!!

I don't know if this is even possible but I want to ask if it is possible.

This is my situation: - I have a master database and a local database, the master database contains all the records of all users, the local database contains only the records of one single user.

  • Both DB Schemas are exactly the same with +/- 150 tables

  • All primary keys are ints with identity_scope = true

The problem is, when using LLBLGen to get an entity (with subentities) from the master database and save this to the local database I should be able to set the primary key (as this is my only reference)

Later on (after updating the local entities) I should be able to re-sync the local entities to the master database, so primary keys should be the same (because all FK-relations should keep working of course)

Shoot! :-)

Grts, Bert

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 24-Dec-2008 14:21:20   

If it's the case that entities always get created in the Main database then it's sent to the local database, then I suggest you remove the Identity_scope from the local databases, so that it uses the Pk values from the mai database.

Otherwise, you might consider having another unique field as a token for synchronization. And then you can just ignore the PKs when saving entities to the local databases.