实体框架错误:无法为表中的标识列插入显式值 [英] Entity Framework error: Cannot insert explicit value for identity column in table

查看:23
本文介绍了实体框架错误:无法为表中的标识列插入显式值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于实体框架错误:无法为表中的标识列插入显式值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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