db exception was unhandled ( {"An error occurred while updating the entries. See the inner exception for details."} )
Question:
I am getting this
(dbexception unhandled by user code)
{"An error occurred while updating the entries. See the inner exception for details."}exception on
System.Data.UpdateException
db.SaveChanges();
Answer:
Sometimes in Entity Framework we get weird exception while inserting,updating
or deleting data but developers are unable to understand the exception it does
not show details where is actually things messed up, so i am sharing a tip that
always put the code of EF operation in try catch while inserting,updating or
deleting like below, and log the exception details in a text file.
try { // Your code... // Could also be before try if you know the exception occurs in SaveChanges db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { var outputLines = new List<string>(); foreach (var eve in e.EntityValidationErrors) { outputLines.Add(string.Format( "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { outputLines.Add(string.Format( "- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines); }