无法更新Entity Framework Core中的标识列 [英] cannot update identity column in Entity Framework Core

查看:692
本文介绍了无法更新Entity Framework Core中的标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在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.

对于Entity Framework Core 2.0,"IsReadOnlyAfterSave"属性为 不推荐使用.使用以下内容:

for Entity framework core 2.0 , The "IsReadOnlyAfterSave" property is deprecated. Use following:

builder.Property(p => p.Id)
    .UseSqlServerIdentityColumn();

builder.Property(p => p.Id)
    .Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;

这篇关于无法更新Entity Framework Core中的标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆