This is a migrated thread and some comments may be shown as answers.

Getting the identity after an insert?

5 Answers 252 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrew Vanek
Top achievements
Rank 1
Andrew Vanek asked on 28 Dec 2009, 04:58 PM
Hello,

I am wondering if it is possible to obtain the identity of an object in the database table after inserting it using OpenAccess. The table I am inserting into has a primary key column that is an auto-incrememted identity. What I would like to do is to perform the insert for a single entry and get the identity of the newly inserted entry in return. This way, I can reference that entry later for an update statement based on the identity. Is this possible?

Thanks,
Andy

5 Answers, 1 is accepted

Sort by
0
devoas
Top achievements
Rank 1
answered on 29 Dec 2009, 07:30 AM
Hi Andy,

I would like to take the opportunity to reply this post.

When any persistence object is saved having identity field, after transaction commit that identity field is automatically filled and you can reference that field and it will give you the value. 

//Customer have two fields Id field : CustomerId which is Identity and Auto-increament  and Customer Name  
 
Customer cst = new Customer(); 
cst.CustomerName = "Test"
scope.Transaction.Begin() 
scope.Add(cst); 
scope.Commit(); 
 
MessageBox.Show("Saved:" +cst.CustomerId.ToString() + " " +cst.CustomerName.ToString()); 
 

Hope this may help.

Thanks,
devoas




0
Andrew Vanek
Top achievements
Rank 1
answered on 29 Dec 2009, 02:17 PM
Thank you for your response devoas. I tried out what you wrote and indeed that does work. Thank you for the assistance.

Regards,
Andy
0
PetarP
Telerik team
answered on 30 Dec 2009, 09:40 AM
Hello Andy,

What Devoas provided is correct. The reason for this behavior is that when auto increment mechanism is chosen Telerik OpenAccess ORM depends on the backend to generate the key rather than generating it itself. Having that in mind the entry you are creating would have to do one round trip to the database so that it gets its ID. This round trip can be achieved either by calling Commit or by calling Flush. The difference between the two methods is that the Flush would keep your transaction active and you will be able to still rollback if you need to.


Kind regards,
Petar
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Llewellyn
Top achievements
Rank 1
answered on 06 Apr 2014, 02:09 PM
What is  scope? I am using the latest Telerik Data Access. Scope does not exist. Please elaborate.
0
Kristian Nikolov
Telerik team
answered on 08 Apr 2014, 11:19 AM
Hello Llewellyn,

The scope object you are seeing in the code snippets is legacy of old releases of Telerik Data Access. It is part of our Classic API which will soon be entirely deprecated.

Currently the Context API is used to perform CRUD operations. The way to access the identity value of an object with backend calculated identity is relatively unchanged. The identity property will return its actual value once the object has been inserted in the database by adding it to the context and then calling the SaveChanges method. The following code snippet is an example of accessing the identity of a newly inserted Car object:
Car car = new Car()
{
    Make = "Audi",
    CarYear = 2012,
    Model = "A6",
    TagNumber = "31-293"
};
context.Add(car);
context.SaveChanges();
int autoIncIdValue = car.CarID;

I hope this helps. Should you have additional questions, feel free to use our forums again.

Regards,
Kristian Nikolov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
Development (API, general questions)
Asked by
Andrew Vanek
Top achievements
Rank 1
Answers by
devoas
Top achievements
Rank 1
Andrew Vanek
Top achievements
Rank 1
PetarP
Telerik team
Llewellyn
Top achievements
Rank 1
Kristian Nikolov
Telerik team
Share this question
or