Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you solve IDENTITY_INSERT is set to off?

You specified your ID column to be Identity in your database. After that you're trying to insert a specific value to that column which is most likely something you don't want to do. Remove the value for ID in your ASP.NET code when inserting, it will be auto-generated for you.

Can we insert value in identity column?

An explicit value for the identity column in table 'Students' can only be specified when a column list is used and IDENTITY_INSERT is ON. In simple words, the error says that since the flag IDENTITY_INSERT is off for the Id column, we cannot manually insert any values.

Can you implicitly insert an identity column in a table?

Yes. That's all you need to do. SET ON, write your code, SET OFF at the end.

What is meant by this error message in the SQL query window Cannot insert explicit value for identity column in table tracks?

This error means you have an identity column in the table, and you're trying to set a value for it.


You're inserting values for OperationId that is an identity column.

You can turn on identity insert on the table like this so that you can specify your own identity values.

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 

don't put value to OperationID because it will be automatically generated. try this:

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

Simply If you getting this error on SQL server then run this query-

SET IDENTITY_INSERT tableName ON

This is working only for a single table of database e.g If the table name is student then query look like this:

SET IDENTITY_INSERT student ON

If you getting this error on your web application or you using entity framework then first run this query on SQL server and Update your entity model (.edmx file) and build your project and this error will be resolved


Be very wary of setting IDENTITY_INSERT to ON. This is a poor practice unless the database is in maintenance mode and set to single user. This affects not only your insert, but those of anyone else trying to access the table.

Why are you trying to put a value into an identity field?