Cannot insert explicit value for identity column i

2019-01-01 07:26发布

问题:

I have the below error when I execute the following script. What is the error about, and how it can be resolved?

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

Error:

Server: Msg 544, Level 16, State 1, Line 1

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

回答1:

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 


回答2:

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

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


回答3:

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?



回答4:

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

SET IDENTITY_INSERT tableName ON

e.g If 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



回答5:

In your entity for that table, add the DatabaseGenerated attribute above the column for which identity insert is set:

Example:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TaskId { get; set; }


回答6:

There are basically 2 different ways to INSERT records without having an error:

1) When the IDENTITY_INSERT is set OFF. The PRIMARY KEY \"ID\" MUST NOT BE PRESENT

2) When the IDENTITY_INSERT is set ON. The PRIMARY KEY \"ID\" MUST BE PRESENT

As per the following example from the same Table created with an IDENTITY PRIMARY KEY:

CREATE TABLE [dbo].[Persons] (    
    ID INT IDENTITY(1,1) PRIMARY KEY,
    LastName VARCHAR(40) NOT NULL,
    FirstName VARCHAR(40)
);

1) In the first example, you can insert new records into the table without getting an error when the IDENTITY_INSERT is OFF. The PRIMARY KEY \"ID\" MUST NOT BE PRESENT from the \"INSERT INTO\" Statements and a unique ID value will be added automatically:. If the ID is present from the INSERT in this case, you will get the error \"Cannot insert explicit value for identify column in table...\"

SET IDENTITY_INSERT [dbo].[Persons] OFF;
INSERT INTO [dbo].[Persons] (FirstName,LastName)
VALUES (\'JANE\',\'DOE\'); 
INSERT INTO Persons (FirstName,LastName) 
VALUES (\'JOE\',\'BROWN\');

OUTPUT of TABLE [dbo].[Persons] will be:

ID    LastName   FirstName
1     DOE        Jane
2     BROWN      JOE

2) In the Second example, you can insert new records into the table without getting an error when the IDENTITY_INSERT is ON. The PRIMARY KEY \"ID\" MUST BE PRESENT from the \"INSERT INTO\" Statements as long as the ID value does not already exist: If the ID is NOT present from the INSERT in this case, you will get the error \"Explicit value must be specified for identity column table...\"

SET IDENTITY_INSERT [dbo].[Persons] ON;
INSERT INTO [dbo].[Persons] (ID,FirstName,LastName)
VALUES (5,\'JOHN\',\'WHITE\'); 
INSERT INTO [dbo].[Persons] (ID,FirstName,LastName)
VALUES (3,\'JACK\',\'BLACK\'); 

OUTPUT of TABLE [dbo].[Persons] will be:

ID    LastName   FirstName
1     DOE        Jane
2     BROWN      JOE
3     BLACK      JACK
5     WHITE      JOHN


回答7:

you can simply use This statement for example if your table name is School. Before insertion make sure identity_insert is set to ON and after insert query turn identity_insert OFF

SET IDENTITY_INSERT School ON
/*
  insert query
  enter code here
*/
SET IDENTITY_INSERT School OFF


回答8:

If you are using liquibase to update your SQL Server, you are likely trying to insert a record key into an autoIncrement field. By removing the column from the insert, your script should run.

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


回答9:

There is pre-mentioned OperationId in your query which should not be there as it is auto increamented

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

so your query will be

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


回答10:

Well I solved mine pretty easy, Check that your Primary Key are the same name with your classes where the only difference is that your primary key have \'ID\' appended to it or specify [Key] on primary keys that are not related to how the class is named.



回答11:

And if you are using Oracle SQL Developer to connect, remember to add /sqldev:stmt/

/sqldev:stmt/ set identity_insert TABLE on;



回答12:

I\'m not sure what the use for the \"Insert Table\" is, but if you\'re just trying to insert some values try:

Insert Into [tablename] (OpDescription,FilterID)
values (\'Hierachy Update\',1);

I had the same error message come up, but I think this should work. The ID should auto-increment automatically as long as it\'s a primary key.



回答13:

The problem raised from using non-typed DBContext or DBSet if you using Interface and implement method of savechanges in a generic way

If this is your case I propose to strongly typed DBContex for example

MyDBContext.MyEntity.Add(mynewObject)

then .Savechanges will work