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 112763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:35:45+00:00 2026-05-11T02:35:45+00:00

We use Guids as primary keys for entities in the database. Traditionally, we’ve followed

  • 0

We use Guids as primary keys for entities in the database. Traditionally, we’ve followed a pattern of letting the database set the ID for an entity during the INSERT, I think mostly because this is typically how you’d handle things using an auto-increment field or whatever.

I’m finding more and more that it’s a lot handier to do key assignment in code during object construction, for two main reasons:

  1. you know that once an object’s constructor has run, all of it’s fields have been initialized. You never have ‘half-baked’ objects kicking around.
  2. if you need to do a batch of operations, some of which depend on knowing an object’s key, you can do them all at once without round-tripping to the database.

Are there any compelling reasons not to do things this way? That is, when using Guids as keys, is there a good reason to leave key assignment up to the database?

Edit: A lot of people have strong opinions on whether or not Guids should be used for PKs (which I knew), but that wasn’t really the point of my question.

Aside from the clustering issue (which doesn’t seem to be a problem if you set your indexes up properly), I haven’t seen a compelling reason to avoid creating keys in the application layer.

  • 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. 2026-05-11T02:35:46+00:00Added an answer on May 11, 2026 at 2:35 am

    I think you are doing just fine by creating them on the client side. As you mentioned, if you let the db do it, you have to find some way (can’t think of any really) to get that key. If you were using an identity, there are calls you can use to get the latest one created for a table, but I’m not sure if such exists for a guid.

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

Sidebar

Related Questions

I've been investigating the use of GUIDs as primary keys in databases. So far,
We use GUIDs for primary key, which you know is clustered by default. When
I am planning on using sequential guids as primary keys/uuids as detailed in the
I'm new to NHibernate. If I use NHibernate to generate GUIDs for inserts to
I have read a lot of articles about whether we should have primary keys
I'm running NHibernate and SQL Server CE I am trying to use GUIDs as
For my primary key and identity in our Clients table I use uniqueidentifier column
Definition of variables in use: Guid fldProId = (Guid)ffdPro.GetProperty(FieldId); string fldProValue = (string)ffdPro.GetProperty(FieldValue); FormFieldDef
Trying to use a guid as a resource id in a rest url but
When saving data in different sql tables, is it best to use the GUID

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.