Adding multiple entities

Posts   
 
    
rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 03-Mar-2006 14:43:43   

Hi,

A quick question, which is probably very easy ??... I have 2 entities to commit to the database. 1 entity references the other with a foreign key...

I can save the first entity, then assign the foreign key to the second entity and save the second entity which is fine.

My question is, is there a way to do this in one "transaction", ie is assign automatically as I probably will have more entities to. I am doing the following but the save is not assigning the foreign key after the Save(). See code below.. I have also lloked at the "UnitOfWork" object which may or may not help me, any pointers would be great

Many thanks

Richard

Transaction t = new Transaction(IsolationLevel.ReadCommitted, "Client Save");

try { //Add the customer for saving work.AddForSave(customer);

t.Add(customer);

customer.Save();

//set the billing
billing.CGuid = customer.CGuid;
billing.AddType = (byte)AddressType.BillingAddress;

//set the shipping
shipping.CGuid = customer.CGuid;
shipping.AddType = (byte)AddressType.ShippingAddress;

//Add the addresses to the unit of work
t.Add(billing);
t.Add(shipping);

billing.Save();
shipping.Save();

t.Commit();

} catch (Exception e) { t.Rollback(); LogManager.WindowsLogError("Customer add failure "+e.Message); return false; } finally { t.Dispose(); }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Mar-2006 15:27:28   

Please check the Saving entities recursively section in the LLBLGen Pro documentation "Using the generated code -> SelfServicing -> Using the entity classes"

rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 03-Mar-2006 15:48:14   

Exactly what I needed.. Thanks

rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 03-Mar-2006 16:32:24   

Walaa wrote:

Please check the Saving entities recursively section in the LLBLGen Pro documentation "Using the generated code -> SelfServicing -> Using the entity classes"

Hi Guys, sorry to be a dunce, but I have looked at this code and read and re-read... I am assuming I am doing this correctly, my relationship between the two tables has cardinality zero-or-more.

I have 1 Customer Entity, which can have multiple address entities.. I am adding to the Address collection on the customer each address. Then performing a Save(true) call on the customer. The foreign key is actually a Guid, but when I examine the address table, what should be the Guid of the Customer is set to Guid.Empty (All zeros). my code is below, not much there but am assuming this is all I need to do. I am guessing that I am really not seeing this properly or I have not got the correct relationship between the 2 tables defined.

Again Many thanks, I really would like to use this method as opposed to saving each entity seperately simple_smile

Richard

customer.Address.Add(billing); customer.Address.Add(shipping);

return customer.Save(true);

rparkins
User
Posts: 66
Joined: 04-May-2005
# Posted on: 03-Mar-2006 17:40:56   

rparkins wrote:

Walaa wrote:

Please check the Saving entities recursively section in the LLBLGen Pro documentation "Using the generated code -> SelfServicing -> Using the entity classes"

Hi Guys, sorry to be a dunce, but I have looked at this code and read and re-read... I am assuming I am doing this correctly, my relationship between the two tables has cardinality zero-or-more.

I have 1 Customer Entity, which can have multiple address entities.. I am adding to the Address collection on the customer each address. Then performing a Save(true) call on the customer. The foreign key is actually a Guid, but when I examine the address table, what should be the Guid of the Customer is set to Guid.Empty (All zeros). my code is below, not much there but am assuming this is all I need to do. I am guessing that I am really not seeing this properly or I have not got the correct relationship between the 2 tables defined.

Again Many thanks, I really would like to use this method as opposed to saving each entity seperately simple_smile

Richard

customer.Address.Add(billing); customer.Address.Add(shipping);

return customer.Save(true);

OK, I replaced the Primary Guids for Primary long and all works ok now... I will still use Guids for references in the URL within the browser, to give some kind of security simple_smile

So my guess is that this recursive process only works on keys that are long not guids... Would love to kow if anyone can shed some light, but at least it is now working.... btw this is a great product simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 04-Mar-2006 11:38:46   

rparkins wrote:

Walaa wrote:

Please check the Saving entities recursively section in the LLBLGen Pro documentation "Using the generated code -> SelfServicing -> Using the entity classes"

Hi Guys, sorry to be a dunce, but I have looked at this code and read and re-read... I am assuming I am doing this correctly, my relationship between the two tables has cardinality zero-or-more.

I have 1 Customer Entity, which can have multiple address entities.. I am adding to the Address collection on the customer each address. Then performing a Save(true) call on the customer. The foreign key is actually a Guid, but when I examine the address table, what should be the Guid of the Customer is set to Guid.Empty (All zeros). my code is below, not much there but am assuming this is all I need to do. I am guessing that I am really not seeing this properly or I have not got the correct relationship between the 2 tables defined.

Again Many thanks, I really would like to use this method as opposed to saving each entity seperately simple_smile

Richard

customer.Address.Add(billing); customer.Address.Add(shipping);

return customer.Save(true);

The customer's GUID is set manually in code? This is important, as the GUID value generated in the db isn't read back, as there's no way to determine what the guid value is after the insert statement.

Frans Bouma | Lead developer LLBLGen Pro