gpt4 book ai didi

mysql - 使用 Entity Framework 更新表时出现错误 :"Cannot update identity column..."

转载 作者:行者123 更新时间:2023-11-30 22:10:11 25 4
gpt4 key购买 nike

使用 visual Studio 2012 v.11 update 4.Net 4.5.5x

我试图从 Gridview 界面简单地更新数据库中的表,但出现上述错误。我的字段是一个整数自动递增标识字段,但我的 PK 是 GUID 的 ID 列。问题是为什么 entityframework 应该尝试更新数据库中在其属性中设置为“身份”的字段?

尝试从同一个 GridView 控件插入或删除同一个表上的记录时,我没有收到任何错误提前发送。

最佳答案

有两种可能:

1st: You are accidentally updating the PK. This is possible since your PK is a GUID as you mentioned.

2nd: EF assumes that it has to create a PK for the record.

第一种情况的解决方案是跟踪过程以查看意外创建PK的位置。对于第二种情况,在 DbContext 中的 PK 上使用以下内容:

 .ValueGeneratedNever(); // EF Core 
Example:
entity.Property(e => e.PK).ValueGeneratedNever();

[DatabaseGenerated(DatabaseGeneratedOption.None)] EF6
public GUID PK {get; set;}

关于mysql - 使用 Entity Framework 更新表时出现错误 :"Cannot update identity column...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com