Getting the identity after an insert?

Thread is closed for posting
6 posts, 0 answers
  1. Andrew Vanek
    Andrew Vanek avatar
    4 posts
    Member since:
    Aug 2009

    Posted 28 Dec 2009 Link to this post


    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?

  2. devoas
    devoas avatar
    71 posts
    Member since:
    Dec 2009

    Posted 29 Dec 2009 Link to this post

    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"
    MessageBox.Show("Saved:" +cst.CustomerId.ToString() + " " +cst.CustomerName.ToString()); 

    Hope this may help.


  3. Andrew Vanek
    Andrew Vanek avatar
    4 posts
    Member since:
    Aug 2009

    Posted 29 Dec 2009 Link to this post

    Thank you for your response devoas. I tried out what you wrote and indeed that does work. Thank you for the assistance.

  4. PetarP
    PetarP avatar
    754 posts

    Posted 30 Dec 2009 Link to this post

    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,
    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.
  5. Llewellyn
    Llewellyn avatar
    1 posts
    Member since:
    Mar 2014

    Posted 06 Apr 2014 in reply to devoas Link to this post

    What is  scope? I am using the latest Telerik Data Access. Scope does not exist. Please elaborate.
  6. Kristian Nikolov
    Kristian Nikolov avatar
    206 posts

    Posted 08 Apr 2014 Link to this post

    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"
    int autoIncIdValue = car.CarID;

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

    Kristian Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top