如何确定IdentityUser类的主键"Id"为IDENTITY(1,1)? [英] How to tell the primary key 'Id' of IdentityUser class is IDENTITY(1,1)?
本文介绍了如何确定IdentityUser类的主键"Id"为IDENTITY(1,1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已按照现在,当我要在用户表中插入数据时,它需要Id字段的数据.但我希望ID字段中的值将由身份自动创建.
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
但我认为应该是
[Id] INT IDENTITY(1,1) NOT NULL
那我该怎么做
[Id] INT IDENTITY(1,1) NOT NULL
推荐答案
您需要在Id
字段上添加DatabaseGenerated(DatabaseGeneratedOption.Identity)
属性:
You need to add DatabaseGenerated(DatabaseGeneratedOption.Identity)
attribute on your Id
field:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
然后添加EF迁移-它将ID字段更改为自动递增.
and then add an EF migration - it will change the ID field to be auto-incremented.
这篇关于如何确定IdentityUser类的主键"Id"为IDENTITY(1,1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文