Spodworld

a collection of technical fixes and other randon stuff

MVC error "Cannot insert explicit value for identity column in table 'TABLE_NAME' when IDENTITY_INSERT is set to OFF."

When doing a simple MVC application, I kept coming across the error:

 

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

Prior to this I had created my table and generated the model/controller etc... but had made a change to the table to have an auto Id with increment of one.

Rather than re-building your model and other classes, the folllowing should help to fix it:

 

  • Edit the .edmx file of the model involved
  • Click on the primary key/index involved
  • Go to the properties window and scroll to the property [StoreGeneratedPattern] and set it to "Identity"
  • Recompile and try again

 

THis worked for me, hopefully it will help you too!

Loading