Turn on IDENTITY_INSERT in Entity Framework, SQL Server 2008? [duplicate]
Asked Answered
P

1

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();

Padriac answered 15/8, 2011 at 10:11 Comment(3)
As gbn already said in response to our duplicate question: don't set the IDENTITY value! Let SQL Server do this for you.Summon
No i have requested for that to be closes as it doesn't mention about entity framework and where i am getting the error.Padriac
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 tablePadriac
S
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.

Summon answered 15/8, 2011 at 10:23 Comment(4)
@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 environmentSummon
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 modelPadriac
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.Midwinter
thanks hat worked, finally got therePadriac

© 2022 - 2024 — McMap. All rights reserved.