I'm taking data from an xml file and when using the entity framework to add that data to my database I'm getting a System.Data.UpdateException. It is caused by a primary key violation, which for the data I'm entering is possible. I'm handling the exception and moving on but I would like to know how to see specifically what PK was in violation.

Any suggestions?


You can use a from linq command to select the entity and apply a where clause for the PK to verify if it exists on the entitycollection, so you can decide if the returned collection has 0 elements then you can add the new element to the entity. Otherwise, the PK already exist, and you can decide to update the relevant entity element or just ignore it.

Hope this helps