问题描述
我已经在AspNetUsers表中添加了一个单独的标识,称为NumericId,它将与GUID一起使用,例如ASP默认具有的ID.
I've added a separate Identification to the AspNetUsers table called NumericId that will serve along with the GUID like ID that ASP has for default.
我已将该属性添加为ApplicationUser类的附加属性:
I've added the property as an additional property of the ApplicationUser class:
public class ApplicationUser : IdentityUser
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int NumericId { get; set; }
}
但是,当我尝试注册或更新用户的详细信息(甚至与numericId无关)时,我总是收到错误消息
However, when I try to register, or update the user's details (that aren't even relevant to the numericId) I keep getting the error
SqlException: Cannot update identity column 'NumericId'.
这可以防止任何更改,并且最终不会更新用户(但它确实注册了一个用户,并且在该部分正确分配了数字ID.但这无关紧要)
which prevents any changes and in the end does not update the user (but it does register one, and Numeric Id is properly assigned on that part. But this is irrelevant)
推荐答案
每个关于GitHub的讨论,对于EF Core 2.0,我们需要使用其他帖子中建议的两行内容.
Per the discussion on GitHub surrounding this issue, for EF Core 2.0 we needed to use both lines suggested in other posts.
builder.Property(p => p.Id)
.UseSqlServerIdentityColumn();
builder.Property(p => p.Id)
.Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
这篇关于无法更新Entity Framework Core中的标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!