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

查看:31
本文介绍了无法更新 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)

推荐答案

asp.net core 3.1 的解决方案

Solution for asp.net core 3.1

modelBuilder.Entity<Type>().Property(u => u.Property).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);

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

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