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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11173562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:52:55  来源:igfitidea点击:

Entity Framework error: Cannot insert explicit value for identity column in table

c#sqlentity-framework

提问by Homero Barbosa

I'm getting this error on EF.

我在 EF 上收到此错误。

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

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

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

在分贝列标识增量并在EF的设计文件,StoreGeneratedPatternidentity为好。似乎 EF 每次尝试保存时都试图插入 0。

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

一些建议说 ID 保留在表上或删除表并重新运行脚本。

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

database

数据库

EF Designer

英孚设计师

采纳答案by ems305

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.

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

回答by thomas

Try this:

尝试这个:

using System.ComponentModel.DataAnnotations.Schema;
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public decimal Identity_Col { get; set; }

The Entity Framework class file adds these lines of code to the Identity column.

实体框架类文件将这些代码行添加到标识列。

回答by Smitty

In EF 6, there is a property of the field/column in your model for doing this: StoreGeneratedPattern.

在 EF 6 中,模型中有一个字段/列的属性可以执行此操作:StoreGeneratedPattern。

Set this to "Identity" in the property dropdown list.

在属性下拉列表中将其设置为“身份”。

(I don't know about EF 4. The above answer, using IsDbGenerated, seems to be for EF 4.)

(我不知道 EF 4。使用 IsDbGenerated 的上述答案似乎适用于 EF 4。)

And this corresponds in the underlying XML to an attribute to the element:

这在底层 XML 中对应于元素的属性:

<Property Name="MyTableId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />

--but you don't need to deal with the XML manually, since you can use the designer.

--但是您不需要手动处理 XML,因为您可以使用设计器。

How this gets messed up isn't clear. I had the problem even after refreshing my model from the database. Perhaps it gets confused if you set the PK on the table, or change its name, after you have already generated the model. (I am using the table/database-first approach, not code first.)

这如何搞砸尚不清楚。即使从数据库刷新我的模型后,我也遇到了问题。如果您在生成模型后在表上设置 PK 或更改其名称,可能会感到困惑。(我使用的是表/数据库优先的方法,而不是代码优先。)

You can't use the above approach of putting the C# attribute on the entity code, because in this situation the entity code is generated by EF. EF is supposed to understand ("by itself") that the field is an identity.

您不能使用上述将 C# 属性放在实体代码上的方法,因为在这种情况下实体代码是由 EF 生成的。EF 应该理解(“本身”)该字段是一个身份。

回答by Amin Saqi

Put these attribs on top of the property which is identity:

将这些属性放在身份属性的顶部:

[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }

回答by Erik

First match in google, so here is my solution:

谷歌的第一场比赛,所以这是我的解决方案:

EF Code first: Because of an auto-increment PK 'id' field AND a guid column, design like this:

EF 代码优先:由于自增 PK 'id' 字段和 guid 列,设计如下:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid FileToken { get; set; }

there was a duplicate identity. I changed it to:

有重复的身份。我把它改成:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[DefaultValue("newid()")]
public Guid FileToken { get; set; }

and the problem went away.

问题就解决了。

Hope it helps you.

希望对你有帮助。

Erik

埃里克

回答by Charly N

I had this issue in my app; and got fixed it changing the property "StoredGeneratedPattern" of the id field to Identity.

我的应用程序中有这个问题;并修复了将 id 字段的属性“StoredGeneratedPattern”更改为 Identity 的问题。

So, Go to the model; look up for the table; click on propierties of the primary key fiel; and change the property.

所以,转到模型;寻找桌子;点击主键字段的属性;并更改属性。

回答by Red Feet

I encountered the same problem and error message in my AspNetCore 2.x application. The only way I could solve it was by removing this line in the ModelBuilder.Entity method of the DbContext class:

我在 AspNetCore 2.x 应用程序中遇到了同样的问题和错误消息。我可以解决它的唯一方法是删除 DbContext 类的 ModelBuilder.Entity 方法中的这一行:

// remove: entity.Property(e => e.Id).ValueGeneratedNever();