viernes, 11 de septiembre de 2009

Linq To SQL: Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

Technology: LinQ to SQL

Problem: When inserting a record using LinQ to SQL in a table that has properly defined a column with an autogenerated ID, on submit linq to sql will thow the error:

Linq To SQL: Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

This problem appeared when i was trying to delete all records of a relationship, and adding new ones.
The relationship table had an Id column, which was not set

Solution:
Apparently, linq to sql "mixed" the old records that i deleted with the new ones, so I solved the problem calling "submitChanges" after deleting the records of the relationship and before adding the new ones.

6 comentarios: