实体框架错误:无法为表中的标识列插入显式值

编程入门 行业动态 更新时间:2024-10-23 10:20:48
本文介绍了实体框架错误:无法为表中的标识列插入显式值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 EF 上遇到此错误.

I'm getting this error on EF.

无法为表中的标识列插入显式值当 IDENTITY_INSERT 设置为 OFF 时为GroupMembers_New".

Cannot insert explicit value for identity column in table 'GroupMembers_New' when IDENTITY_INSERT is set to OFF.

Db 上的列是标识增量,在 EF 设计文件中,StoreGeneratedPattern 也是 identity.每次我尝试保存时,EF 似乎都试图插入 0.

The column on the Db is identity increment and on the EF design file, StoreGeneratedPattern is identity as well. Seems like EF is trying to insert 0 every time I try to save.

有些建议说 ID 是保留在表上的,或者删除表并重新运行脚本.

Some suggestions says ID is reserved on tables or drop the table and rerun the scripts.

有什么想法吗?

这是一些代码:

GroupMember groupMember = new GroupMember(); groupMember.GroupId = group.Id; groupMember.UserId = (new UserId(group.Owner)); //groupMember.Id = _groupContext.GroupMembers.Count(); group.GroupMembers.Add(groupMember); _groupContext.SaveChanges();

推荐答案

我以前遇到过这个问题.此错误意味着您正在尝试将值显式分配给数据库自动分配的列.

I have run into this before. This error means you are trying to assign a value explicitly to a column where the database automatically assigns it.

建议:更新您的 edmx 文件以反映您可能在数据库中所做的任何更改.如果数据库自动分配值,您应该在设计器文件中的该属性下看到IsDbGenerated=true"属性.如果没有,您可以手动添加.

Suggestion: Update your edmx file to reflect any changes you may have made in the database. If the database automatically assigns the value, you should see the "IsDbGenerated=true" attribute in your designer file under that property. If it's not there, you can add it manually.

更多推荐

实体框架错误:无法为表中的标识列插入显式值

本文发布于:2023-11-16 01:34:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1599799.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   标识   框架   错误   显式值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!