Search code examples
asp.netasp.net-identity

How to tell the primary key 'Id' of IdentityUser class is IDENTITY(1,1)?


I have changed the type of primary key of IdentityUser class from string to int following this.

Now when I go to insert data in User table, it wants data for Id field. But I want that the value in Id field will be automatically created by Identity.

In server explorer when I open table definition, it shows

[Id] INT NOT NULL

but I think it should be

[Id] INT IDENTITY(1,1) NOT NULL

So what should I do to make

[Id] INT IDENTITY(1,1) NOT NULL

Solution

  • You need to add DatabaseGenerated(DatabaseGeneratedOption.Identity) attribute on your Id field:

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    

    and then add an EF migration - it will change the ID field to be auto-incremented.