Identity after Insert

Posts   
 
    
briankb
User
Posts: 51
Joined: 03-Jun-2007
# Posted on: 04-Jun-2007 04:33:59   

I searched for this on the forum but could not find anything specific.

This may be a stupid question, if so I apologize in advance.

How is getting an identity after insert handled?

I have a project that I would like to port to .net and use LLBLGen to help me do it. The user data is split into three tables (users,usercontacts,userlocations). After a user is inserted the newly created row's identity is stored and then used to insert a related row for the usercontacts and userlocations tables. This is all old school and currently hand coded in ASP using the open connection to help retrieve the identity in MySQL database.

If possible could someone point me in the right direction for getting the identity after an insert for MySQL and SQL Server.

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Jun-2007 08:10:32   

Hi Brian, If you are using Adapter Template (copied from LLBLGenPro User Reference Manual):

public bool SaveEntity(SD.LLBLGen.Pro.ORMSupportClasses.IEntity2 entityToSave, bool refetchAfterSave) Member of SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase

Summary: Saves the passed in entity to the persistent storage. If the entity is new, it will be inserted, if the entity is existent, the changed entity fields will be changed in the database. Will do a recursive save. Will pass the concurrency predicate returned by GetConcurrencyPredicate(ConcurrencyPredicateType.Save) as update restriction.

Parameters: entityToSave: The entity to save refetchAfterSave: When true, it will refetch the entity from the persistent storage so it will be up-to-date after the save action.

Returns: true if the save was succesful, false otherwise.

Remarks: Will use a current transaction if a transaction is in progress

For example:

// start a transaction
adapter.StartTransaction(IsolationLevel.RepeatableRead, "TestTrans");

// you are doing things here
//....
// save data and automatically update with the entity that your DB identity just generated 
adapter.SaveEntity(customer, true);

// ... doing other things...
// persist changes...
adapter.Commit();

If you are using SelfServicing Template, when you YourEntity.Save(), YourEntity automatically will be updated with the just generated identity value.

P.S. for future posts, please provide more info about your scenario (http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=9076) so we can help you in the better way wink

David Elizondo | LLBLGen Support Team
briankb
User
Posts: 51
Joined: 03-Jun-2007
# Posted on: 04-Jun-2007 08:40:53   

Thank you for the quick response. I'm just getting started, trying my first project with LLGL tonight. I am new to OOP and your second example is clear to me but I'll have to study the first a bit more to understand it.

Thanks again!