当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?

Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)

错误:

服务器:Msg 544,级别16,状态1,线路1 当IDENTITY_INSERT设置为OFF时,无法为表'table'中的标识列插入显式值。


当前回答

要非常小心地将IDENTITY_INSERT设置为ON。这是一种糟糕的实践,除非数据库处于维护模式并设置为单用户。这不仅会影响您的插入,还会影响试图访问表的其他人的插入。

为什么要在单位字段中输入一个值?

其他回答

This occurs when you have a (Primary key) column that is not set to Is Identity to true in SQL and you don't pass explicit value thereof during insert. It will take the first row, then you wont be able to insert the second row, the error will pop up. This can be corrected by adding this line of code [DatabaseGenerated(DatabaseGeneratedOption.Identity)] in your PrimaryKey column and make sure its set to a data type int. If the column is the primary key and is set to IsIDentity to true in SQL there is no need for this line of code [DatabaseGenerated(DatabaseGeneratedOption.Identity)] this also occurs when u have a column that is not the primary key, in SQL that is set to Is Identity to true, and in your EF you did not add this line of code [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

在我的例子中,我已经在modelBuilder的上下文中设置了另一个属性作为键。

modelBuilder.Entity<MyTable>().HasKey(t => t.OtherProp);

我必须设置正确的id

 modelBuilder.Entity<MyTable>().HasKey(t => t.Id);

只需删除拖放到.dbml文件中的表,然后重新拖动它们。清洁方案>重建方案>重建方案。

这对我很管用。

我没有做我自己的表格,我是用VS和SSMS,我遵循这个链接为ASP。净身份:https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

设置该字段的身份增量时,不能在“OperationID”列中插入数据。

不要在该字段中输入值,它将自动设置。

Insert table(OpDescription,FilterID)
values ('Hierachy Update',1)

在我的例子中,问题是,当我试图更新记录时,我自己从代码中指定了自增ID。 在从新记录创建中删除ID属性之后,一切都正常了。