2

I am supposed to generate a partial random unique id to be stored as an identifier for users.

Criteria:

  1. 8 digits
  2. First 4 digits is of my own (for eg. the year)
  3. Last 4 digits can be anything random.

How do I use entity framework to make sure this id is unique? I don't want to have a loop that generates then check the database. Can something like this be done in 1 database call?

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406

1 Answers1

2

The only way to do this in a single call would be to call a stored procedure that generates the ID and checks uniqueness.

Dave Hogan
  • 3,201
  • 6
  • 29
  • 54
  • I agree, please see the following previous question, which shows how to implement this using a stored procedure. http://stackoverflow.com/questions/5924258/what-is-the-best-way-to-manually-generate-primary-keys-in-entity-framework-4-1-c – Paige Cook Jan 11 '12 at 16:00