I have an appication that allows multiple users and a database table which has 2 IDs as a compound key. These IDs are also foreign keys from another table. So when 2 users try to add an entry to this tabel with the same IDs one of them gets an UpdateException because of Primary Key constaint violation. I already found out that it should be handled like this:
try { result = base.SaveChanges(options); } catch (UpdateException ex) { SqlException innerException = ex.InnerException as SqlException; if (innerException != null && innerException.Number == 2627 || innerException.Number == 2601) { // handle here } else { throw; } } But what do I actually do on the "//Handle here" part. I tried refreshing the object but it is in the "Added" state and therefor can not be refreshed. What I whant it to do is: Acknowledge that there is already an object with these IDs, drop its object that it wanted to insert and load the existing object from the database. How can I do that?