[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-02-25
8
Medium Priority
?
1,255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 23

Assisted Solution

by:Rajkumar Gs
Rajkumar Gs earned 400 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 800 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
 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 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