how to create automatic increment in class model ?

sblb 1,231 Reputation points
2023-05-05T18:22:50.21+00:00

Hi,

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

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

Accepted answer
  1. Zhi Lv - MSFT 32,446 Reputation points Microsoft Vendor
    2023-05-08T07:50:03.7933333+00:00

    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:

    image1

    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...

    image2

    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; }
        
    
            #region
    
            //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; }
            #endregion
            protected override void OnModelCreating(ModelBuilder builder)
            {
                base.OnModelCreating(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);
       
                #region
                //builder.Entity<Account>(entity => { entity.ToTable("Accounts"); });
                //builder.Entity<Customer>(entity => { entity.ToTable("Customers"); });
                //builder.Entity<Professional>(entity => { entity.ToTable("Professionals"); });
                #endregion
            }
    

    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,

    Dillion


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.