Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:25:30+00:00 2026-06-17T11:25:30+00:00

For a table that has an identity: [AutoIncrement] public int Id { get; set;}

  • 0

For a table that has an identity:

    [AutoIncrement]
    public int Id { get; set;}

When inserting a new row into the database, what is the best way to retrieve the Id of the object?

For example:

db.Insert<> (new User());

The value of the Id is 0 after the insert, but in the database this obviously is not the case. The only possibility I can see is the following:

Id = (int)db.GetLastInsertId();

However I don’t believe this would be a safe call to make. If there are 100’s of inserts happening at the same time, an Id for another insert may be returned. In EF when you do an insert the Id is set for you.

Does anyone know the best way to go about this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:25:31+00:00Added an answer on June 17, 2026 at 11:25 am

    In ServiceStack.OrmLite v4 which defaults to using parameterized queries there are a couple of options in db.Save() which automatically populates the AutoIncrement Id, e.g:

    db.Save(item);
    item.Id //populated with the auto-incremented id
    

    Otherwise you can select the last insert id using:

    var itemId = db.Insert(item, selectIdentity:true);
    

    Here are more examples showcasing OrmLite’s new API’s.


    For OrmLite v3

    The correct call is db.GetLastInsertId() which for SQL Server under the hood for example calls SELECT SCOPE_IDENTITY() which returns the last inserted id for that connection.

    This is safe because all the other concurrent inserts that might be happening are using different DB connections. In order for anyone else to use the same connection it needs to be disposed of and released back into the pool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
I have an invoice table that has InvoiceID (int identity ) Primary key. and
I have a pictures table that has the following columns: PICTURE_ID int IDENTITY(1000,1) NOT
I have a table that stores user information. The table has a userid (identity)
I have a table that has a composite key that consists of two int
In SQL Server, how can I insert data into a table that has just
Scenario: I have a fairly generic table (Data), that has an identity column. The
I have a table that has an identity column as the Primary Key. None
I have a Login Table that has a Identity field Id as the Primary
I am working on a database that has an existing Individuals table that every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.