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

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

错误:

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


当前回答

如果你在使用带有sequize -typescript npm的sql-server时遇到这个问题,请确保将@AutoIncrement添加到ID列:

  @PrimaryKey
  @AutoIncrement
  @Column
  id!: number;

其他回答

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

/sqldev:stmt/ set identity_insert TABLE on

首先转到所需的表名

步骤2 ->右击表名

选择设计

单击列名

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

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

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

步骤1) 步骤2)

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

如果您正在使用liquibase更新SQL Server,则可能试图将记录键插入到autoIncrement字段中。通过从插入中删除列,脚本应该可以运行。

<changeSet id="CREATE_GROUP_TABLE" >
    <createTable tableName="GROUP_D">
        <column name="GROUP_ID" type="INTEGER" autoIncrement="true">
            <constraints primaryKey="true"/>
        </column>
    </createTable>
</changeSet>

<changeSet id="INSERT_UNKNOWN_GROUP" >
    <insert tableName="GROUP_D">    

        <column name="GROUP_ID" valueNumeric="-1"/>
 ...
    </insert>
</changeSet>

如果你在SQL服务器上得到这个错误,然后运行这个查询-

SET IDENTITY_INSERT tableName ON

这只适用于数据库的一个表 例:如果表名是student,那么查询如下:

SET IDENTITY_INSERT student ON

如果你在你的web应用程序或你使用实体框架得到这个错误,那么首先在SQL服务器上运行这个查询,并更新你的实体模型(。Edmx文件)并构建您的项目,此错误将被解决