How to tell the primary key 'Id' of Identi

2019-07-31 12:52发布

问题:

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

回答1:

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.