Solved

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

Posted on 2011-02-25
8
1,212 Views
Last Modified: 2012-05-11
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
Comment
Question by:newbieweb
8 Comments
 
LVL 23

Assisted Solution

by:Rajkumar Gs
Rajkumar Gs earned 100 total points
ID: 34982463
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
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 34982473
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
 

Author Comment

by:newbieweb
ID: 34982498
> 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
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 34982563
>>No, I am letting that value get set by the database.


Then it should be working irrespective of that setting.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:newbieweb
ID: 34982662
For the record, where can I find that setting in SQL Server 2008?
0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 200 total points
ID: 34982800
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
 

Author Comment

by:newbieweb
ID: 34985453
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
 

Author Closing Comment

by:newbieweb
ID: 34985454
Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now