get ID of Newly Created Entity

Posts   
 
    
p3mek
User
Posts: 68
Joined: 19-Feb-2007
# Posted on: 30-Mar-2007 04:07:59   

Sure this has come up previously but I can't seem to find a sufficient answer using the forum search.

What is the most efficient way to retreive the ID of a newly Created Entity. I would prefer not to use a Stored Procedure.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 30-Mar-2007 07:36:33   

From "Using the Entity Classes" in the documentation.

Self servicing

The code is aware of sequences / identity columns and will automatically set the value for an identity / sequence column right after the Save() method returns, thus is available in the next statement after a call to Save(). If you're using a database which uses sequences, like Oracle or Firebird, be sure to define the field which should be used with a sequence as identity in the entity editor.

Adapter

The code is aware of sequences / identity columns and will automatically set the value for an identity / sequence column after the entity is physically saved inside SaveEntity(). The new value for sequenced columns is available to you after SaveEntity(), even though you haven't specified that the entity has to be refetched. This can be helpful if you want to refetch the entity later. If you're using a database which uses sequences, like Oracle or Firebird, be sure to define the field which should be used with a sequence as identity in the entity editor. Because the entity saved is new (customer.IsNew is true), SaveEntity() will use an INSERT query. After a successful save, the IsNew flag is set to false and the State property of the Fields object of the saved entity is set to EntityState.Fetched (if the entity is also refetched) or EntityState.OutOfSync.

ps. Please follow the posting gudelines posted in the sticky at the top of the forum so that we may better anwer your questions in the future.

p3mek
User
Posts: 68
Joined: 19-Feb-2007
# Posted on: 04-Apr-2007 03:54:56   

Thanks

I knew this was an easy one for you.