Does anyone have a simple way of handling this exception when updating a record to one that already exists in the database?
Asked
Active
Viewed 544 times
1
-
possible duplicate of [Duplicate key exception from Entity Framework?](http://stackoverflow.com/questions/3967140/duplicate-key-exception-from-entity-framework) – Brad Christie Mar 02 '15 at 22:37
1 Answers
1
Try this:
catch (UpdateException ex)
{
SqlException innerException = ex.InnerException as SqlException;
if (innerException != null && innerException.Number == ??????)
{
//Place you exception code handling here..
}
else
{
throw; //(bubble up)
}
}
This is a simple solution, but you may have issues in the future should the error number change which is unlikely).

marc_s
- 732,580
- 175
- 1,330
- 1,459

Rory Herman
- 11
- 2
-
1If you're going to [copy an answer](http://stackoverflow.com/a/3967158/298053) (down to the number of question marks), please be sure to cite it. – Brad Christie Mar 02 '15 at 22:35