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

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

问题描述

我在EF上收到此错误。


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


Db上的列是身份增量,EF设计文件 StoreGeneratedPattern 也是身份。似乎像EF正试图在每次尝试保存时插入0。



有些建议表示ID保留在表上或放下表并重新运行脚本。 >

任何想法?



以下是一些代码:

  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();



解决方案

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



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


I'm getting this error on EF.

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

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.

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

Any ideas?

Here's some code:

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.

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天全站免登陆