问题
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
this is working only single table of databse
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:
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
回答6:
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; }
回答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:
Another situation is to check that the Primary Key is the same name as with your classes where the only difference is that your primary key has an 'ID' appended to it or to specify [Key] on primary keys that are not related to how the class is named.
回答11:
The best solution is to use annotation GeneratedValue(strategy = ...)
, i.e.
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column ...
private int OperationID;
it says, that this column is generated by database using IDENTITY strategy and you don't need to take care of - database will do it.
回答12:
- 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)]
回答13:
And if you are using Oracle SQL Developer to connect, remember to add /sqldev:stmt/
/sqldev:stmt/ set identity_insert TABLE on;
回答14:
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.
回答15:
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
回答16:
Simply delete the tables that are dragged into your .dbml file and re-drag them again. Then Clean solution>Rebuild solution> Build solution.
Thats what worked for me.
I didnt made the table on my own, I was using VS and SSMS, I followed this link for ASP.NET Identity:https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project
来源:https://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity