It may be a bit late, but for those who have an ASP.NET database update error, to find the details, the InnerException error handler is extremely useful and detailed.
try { db.SaveChanges(); } catch (System.Data.UpdateException ex) { Console.WriteLine(ex.InnerException); } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext { Console.WriteLine(ex.InnerException); } catch (Exception ex) { Console.WriteLine(ex.InnerException); throw; }
T-SQL in the database prevents the insertion of duplicated cities.
USE [ModelFirstApplication] GO ALTER TABLE [dbo].[Addresses] ADD CONSTRAINT [UQ_Address_City] UNIQUE NONCLUSTERED ([City] ASC) GO
So, if the user tries to insert "Spokane" a second time into the address table in this demo application, the above exception handler reports
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint 'UQ_Address_City'. Cannot insert duplicate key in object 'dbo.Addresses'. The duplicate key value is (Spokane). The statement has been terminated.
Knowing which of the exception handlers to use is pretty good, and your question is clear enough.
source share