1

Possible Duplicate:
How to Turn on IDENTITY_INSERT in SQL Server 2008?

Trying to insert data via entity framework into a SQL Server 2008 database. I get error

IDENTITY_INSERT is set to OFF

from the application.

I have ran query SET IDENTITY_INSERT Database. dbo. Baskets ON

And got the message back Command(s) completed successfully. But error still occur...?

storeDB.Baskets.Add(cartItem);          
storeDB.SaveChanges();

Occurs at storeDB.SaveChanges();

Community
  • 1
  • 1
Beginner
  • 28,539
  • 63
  • 155
  • 235
  • 1
    As gbn already said in response to our duplicate question: don't set the IDENTITY value! Let SQL Server do this for you. – marc_s Aug 15 '11 at 10:17
  • No i have requested for that to be closes as it doesn't mention about entity framework and where i am getting the error. – Beginner Aug 15 '11 at 10:18
  • I have set it on SQL server, but i am still getting the error. Thats why i am assuming its to do with the way entity framework Adds to table – Beginner Aug 15 '11 at 10:18

1 Answers1

6

When using an identity column, you need to make sure you have set this appropriately in your EDMX (Entity Framework) model. Your column should look like this:

enter image description here

Is your column set to be StoreGeneratedPattern = Identity and Entity Key = True ?? If not - try that!

That's the default that should be mapped automatically, when that situation existed at the time you created the EDMX model. If you changed your column in SQL Server after you've created the model and you didn't update the model, then you might have a discrepancy that could lead to such errors.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • @beginner: if you use Entity Framework, you should have an `YourMOdel.edmx` file somewhere in your solution. That's where the tables are mapped. Open the designer, find the table in question, click on the column that's the IDENTITY column and that'll show this properties dialog in your Visual Studio environment – marc_s Aug 15 '11 at 10:29
  • I belive the Add function is sending a value to the column which is set to auto increment automatically. How do i stop it from sending a value to it without taking ti out of the model – Beginner Aug 15 '11 at 10:32
  • 3
    Which approach do you use? Database-First, Model-First or Code-First? In the first two cases, there should be an .edmx file, in the last you should annotate the Basket's ID property with the [Key]-attribute. – J. Tihon Aug 15 '11 at 10:34
  • 1
    thanks hat worked, finally got there – Beginner Aug 15 '11 at 10:37