• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1262
  • Last Modified:

{"Cannot insert ...when IDENTITY_INSERT is set to OFF."}

Using Entity Framework 4.0 and SQL Server 2008 I got the following exception:

{"Cannot insert explicit value for identity column in table 'notifications' when IDENTITY_INSERT is set to OFF."}

Any idea why?

newbieweb
0
newbieweb
Asked:
newbieweb
4 Solutions
 
Rajkumar GsSoftware EngineerCommented:
Your table contains a IDENTITY column - It could be primary key itself.
When you are trying to insert a data, if you are passing value for this IDENTITY column also, this error could occur - Because IDENTITY column data is automatically inserted by incrementing
0
 
AndyAinscowCommented:
Because the setting in the database IDENTITY_INSERT == OFF does not allow it.  You need to set IDENTITY_INSERT to ON for it to be allowed
0
 
newbiewebAuthor Commented:
> if you are passing value for this IDENTITY column also
No, I am letting that value get set by the database.

> You need to set IDENTITY_INSERT to ON for it to be allowed
How do I do that?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
AndyAinscowCommented:
>>No, I am letting that value get set by the database.


Then it should be working irrespective of that setting.
0
 
newbiewebAuthor Commented:
For the record, where can I find that setting in SQL Server 2008?
0
 
8080_DiverCommented:
newbieweb,

For the record, you really shouldn't be playing with that setting.

I suspect that your query looks something like:
INSERT INTO table
SELECT *
FROM anothertable;

or

INSERT INTO table
SELECT a list of values;

Open in new window


If you try to use the INSERT INTO table without specifying the columns you are inserting into, then the query engine assumes that you are going to insert into all of the columns . . . including the identity column.

try changing your INSERT query to look like:
INSERT INTO table
(
list your columns
)
VALUES
(
list your values
);

or

INSERT INTO table
(
list your columns
)
SELECT your values to be inserted;

Open in new window

0
 
newbiewebAuthor Commented:
All great advice.  Thanks.

It turns out that I have modified the database to have the Indentity set to Yes on the primary ye, but forgot to import it it into the edmx file.  Once I imported, it worked.

0
 
newbiewebAuthor Commented:
Thanks.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now