当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?
Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)
错误:
服务器:Msg 544,级别16,状态1,线路1 当IDENTITY_INSERT设置为OFF时,无法为表'table'中的标识列插入显式值。
当我执行下面的脚本时,我有以下错误。错误是关于什么,如何解决?
Insert table(OperationID,OpDescription,FilterID)
values (20,'Hierachy Update',1)
错误:
服务器:Msg 544,级别16,状态1,线路1 当IDENTITY_INSERT设置为OFF时,无法为表'table'中的标识列插入显式值。
当前回答
请注意,如果您用;结束每一行,SET IDENTITY_INSERT mytable ON命令将不适用于以下行。
即。 像这样的查询
SET IDENTITY_INSERT mytable ON;
INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole');
给出错误 当IDENTITY_INSERT设置为OFF时,无法为表'mytable'中的身份列插入显式值。
但是像这样的查询是可以的:
SET IDENTITY_INSERT mytable ON
INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole')
SET IDENTITY_INSERT mytable OFF;
SET IDENTITY_INSERT命令似乎只适用于事务,而;将表示事务的结束。
其他回答
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)]
设置该字段的身份增量时,不能在“OperationID”列中插入数据。
不要在该字段中输入值,它将自动设置。
Insert table(OpDescription,FilterID)
values ('Hierachy Update',1)
请注意,如果您用;结束每一行,SET IDENTITY_INSERT mytable ON命令将不适用于以下行。
即。 像这样的查询
SET IDENTITY_INSERT mytable ON;
INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole');
给出错误 当IDENTITY_INSERT设置为OFF时,无法为表'mytable'中的身份列插入显式值。
但是像这样的查询是可以的:
SET IDENTITY_INSERT mytable ON
INSERT INTO mytable (VoucherID, name) VALUES (1, 'Cole')
SET IDENTITY_INSERT mytable OFF;
SET IDENTITY_INSERT命令似乎只适用于事务,而;将表示事务的结束。
在我的情况下,我插入了比表中定义的更多的字符。
在我的表列用nvarchar(3)定义,我传递了超过3个字符,同样的错误消息来了。
这不是答案,但在某些情况下可能是类似的问题
我不确定“插入表”的用途是什么,但如果你只是想插入一些值,请尝试:
Insert Into [tablename] (OpDescription,FilterID)
values ('Hierachy Update',1);
我有同样的错误消息出现,但我认为这应该工作。只要是主键,ID就应该自动递增。