How do I generate a primary key that is not random?

Chad Lomax

I want to be able to set the value of my primary key to an auto increment number. At the moment it generates a random key such as 13917c50-6b8c-4405-82ce...

I want to be able to tell the system "Hey, I want request number to start at 1 (or 1000 or whatever) and auto increment every time a record is saved."

public class Request
{
    [Key]
    public string request_no { get; set; }

I expect, as a record is saved, for a request number of my choosing to be auto generated.

Ali Adlavaran

Use DatabaseGenerated attribute:

public class Request
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int request_no { get; set; }
}

Also if you want to create seed for request_no then you should use custom database initializer and execute DBCC CHECKIDENT query:

DBCC CHECKIDENT ('Request', RESEED, 1000)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL: How to generate a unique value for a Primary Key

How do I map this composite primary key?

How do I generate random numbers in Dart?

How do I generate a random int number?

How do i get the primary key of a sequelize model?

How do I alter an existing primary key in a table?

How do I get the last value of primary key in cassandra(php)?

How do I set a unique primary key in Realm?

How do I query DynamoDB with non primary key field?

How do I cluster my PRIMARY KEY in postgres

How do I specify a Primary Key in an INSERT INTO statement?

How do I generate random doubles in an Array?

How do I generate a random num::BigUint?

How do I increase a secondary ID (not the primary key) using linq?

How to concatenate primary key with random number in laravel?

How do I get an Object from the Primary Key

Django post primary key as slug? How do I reference the primary key in models.py?

How do I use Linqpad to insert record with specified primary key?

Generate a random alphanumeric string as a primary key for a model

How do i go about inserting primary key value into another table primary key?

How do I update a row in Yii without using the primary key?

How do I remove the primary key on a table in Access?

How generate the primary key from XML in Hibernate?

How do I make a primary key without auto-increment?

In Django, how do I create a serializer that will auto-generate a primary key for a member of my model?

How do I use a primary key in Bootstrap-table-vue?

How do I auto-increment a alphanumeric string primary key?

NodeJS: How do I generate a random hex key for password reset?

How do I add a primary key to a column, an existing data table?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive