saving entities between two databases

Posts   
 
    
ledaker
User
Posts: 8
Joined: 26-Oct-2008
# Posted on: 03-Sep-2009 11:09:21   

Hi, I have two databases with the same schema and name, which reside on two different servers, when I attempt to load an entity from the first database and save it to the second database (without make any changes in entity) the operation fails (the entity is not saved). However, if I make some changes in this entity then all will be ok (the entity is saved). This is an example:


DbUtils.ActualConnectionString=”"data source=database1…; initial catalog= dbname;..”;
……..
//load an entity from first database
CustomerEntity customer = new CustomerEntity(3131);
DbUtils.ActualConnectionString=”data source=database2…..; initial catalog= dbname;……………….”;
Customer.Save(); //<-- fails
// if I make changes
customer.Address = "1, Bar drive";
customer.City = "Silicon Valey";
customer.CompanyName = "Foo Inc.";
customer.ContactName = "John Coder";
customer.ContactTitle = "Owner";
customer.Country = "USA";
customer.Fax = "(604)555-1233";
customer.Phone = "(604)555-1234";
customer.PostalCode = "90211";

customer.save() ; //<-- it’s ok

What’s wrong please?

Thank you in advance.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 03-Sep-2009 11:23:10   

Please tyr the following:

DbUtils.ActualConnectionString=”"data source=database1…; initial catalog= dbname;..”;
……..
//load an entity from first database
CustomerEntity customer = new CustomerEntity(3131);
DbUtils.ActualConnectionString=”data source=database2…..; initial catalog= dbname;……………….”;

Customer.IsNew = true;
foreach(var field in Customer.Fields)
{
   field.IsChanged = true;
}

Customer.Save(); 
ledaker
User
Posts: 8
Joined: 26-Oct-2008
# Posted on: 03-Sep-2009 14:06:36   

Hi walaa, I've tried that, but without success confused and I notice this, when there are a real changes in some fields the program crash any other solution, plz?

PS: my entity contains related entities from other tables and I use Save(true) to updates all entities.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Sep-2009 20:18:42   

Does the record exist on the second database? For example, Do you want to perform an insert or an update on the second database?

Also, I think the problem is that the fisrt database name is used in the save where the second one should be used. So

  1. Please try this. At config file put this code, so the database name at connectionstring is used. http://llblgen.com/TinyForum/Messages.aspx?ThreadID=13082&StartAtMessage=0&#72683

  2. Check that the generated sql emit the correct query in both queries (fetch and save).

(Edit) Please note that for these kind of things is better to use Adapter template set.

David Elizondo | LLBLGen Support Team
ledaker
User
Posts: 8
Joined: 26-Oct-2008
# Posted on: 06-Sep-2009 11:07:54   

Hi all, thank you for your response, I use the second database for performing a backup of a valid data providing from the first database so if a record exists on the second DB I update it (after making some corrections) else I insert it. the problem is if there is no changes in the original record (all informations are OK) then nothing is saved on the second DB.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Sep-2009 02:59:16   

Ohh, then this is what you must do:

  1. Fetch the graph from database1 (D1). At this point all fields are marked as UNCHANGED and entities in the graph are NOT NEW

  2. Clone the graph. (http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7568) . After this, entities in the graph are ready to be INSERTED as new entities.

  3. Check in the database2 (D2) if the entity exists. If the entity exists you should change the IsNew property to FALSE on all entities in the graph. You can modify the clone routine a bit so can pass this flag to the CloneEntity method.

  4. Perform the save on D2.

David Elizondo | LLBLGen Support Team
ledaker
User
Posts: 8
Joined: 26-Oct-2008
# Posted on: 22-Sep-2009 11:45:47   

Thank you very much, now it works like a charm smile