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

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

错误:

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


当前回答

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)]

其他回答

如果您使用Oracle SQL Developer进行连接,请记住添加/sqldev:stmt/

/sqldev:stmt/ set identity_insert TABLE on

您正在为OperationId插入值,这是一个标识列。

您可以像这样在表上打开标识插入,以便您可以指定自己的标识值。

SET IDENTITY_INSERT Table1 ON

INSERT INTO Table1
/*Note the column list is REQUIRED here, not optional*/
            (OperationID,
             OpDescription,
             FilterID)
VALUES      (20,
             'Hierachy Update',
             1)

SET IDENTITY_INSERT Table1 OFF 

不要给OperationID赋值,因为它会自动生成。试试这个:

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

每个人都评论SQL,但是在EntityFramework中发生了什么?我看了整篇文章,没人能解出EF。所以几天后,我找到了一个解决方案: 在EF Core的上下文中创建模型有这样一条指令:modelBuilder。实体<客户端>(实体=>{实体。属性(e => e.d id).ValueGeneratedNever();

这也会产生错误,解决方案:你必须通过ValueGeneratedOnAdd()改变它的工作!

首先转到所需的表名

步骤2 ->右击表名

选择设计

单击列名

步骤5)进入列属性,然后将No设置为Identity Specification

[注意:插入到显式值后,如果你想,你可以恢复到标识规范为true,然后它会再次生成值]

如果你使用SQL server管理studio你可以使用下面的方法

步骤1) 步骤2)