how to create automatic increment in class model ?

sblb 1,166 Reputation points


I've a table with the class model below, I would like to know how I can have a automatic increment when a new ActionItemId is created?

I don't understand why the increment of ActionItemId is 1 and after 1002 ???

Thanks in advance to your help!

  public class ActionItem

        public int ActionItemId { get; set; }
        public string? Tilte { get; set; }
        public string? DescriptionA { get; set; }

User's image

A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,456 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,056 Reputation points Microsoft Vendor

    Hi @sblb

    By default, for the primary key, the identity key's identity seed is 1 and identity increment is 1. You can view the table design via SSMS and check the identity setting.

    User's image

    why the increment of ActionItemId is 1 and after 1002 ???

    If you are using the default identity setting (both the identity seed and identity increment is 1), after inserting the first item(ActionItemId is 1), you might installed 1001 items in the ActionItems and these items are inserted has been removed or installed fail. So, when insert the items, the next item's ActionItemId is 1002, 1003...

    Like this:


    If the identity increment is set to 1001 (check the table design, and the T-SQL script: Identity(1,1001)), after insert the first item(ActionItemId is 1), the next item's ActionItenId is 1002, 2003, 3004...


    About how to set the identity increment, you can change it from the table design: directly change the T-SQL script and then click the Update button to change the database.

    User's image

    Besides, you can also set identity column in the DbContext's OnModelCreating method, like this:

        public class ApplicationDbContext : IdentityDbContext
            public DbSet<ActionItem> ActionItems { get; set; }
            //public DbSet<Customer> Customers { get; set; }
            //public DbSet<Professional> Professionals { get; set; }
            //public DbSet<Account> Accounts { get; set; }
            public DbSet<A> A { get; set; }
            public DbSet<B> B { get; set; }
            public DbSet<C> C { get; set; }
            protected override void OnModelCreating(ModelBuilder builder)
                //set the primary key
                builder.Entity<ActionItem>().HasKey(c => c.ActionItemId);
                //set the identity column: identity increment and identity seed
                builder.Entity<ActionItem>().Property(c => c.ActionItemId).UseIdentityColumn(1, 1001);
                //builder.Entity<Account>(entity => { entity.ToTable("Accounts"); });
                //builder.Entity<Customer>(entity => { entity.ToTable("Customers"); });
                //builder.Entity<Professional>(entity => { entity.ToTable("Professionals"); });

    Then, the generated table design as below:

    User's image

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,


0 additional answers

Sort by: Most helpful