Thursday, June 1, 2017

How to see 'EntityValidationErrors' property for more details

Exception Message
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details


Code Behind(cs) file
                try
                {
                    // Your code...

                    dbcontext.SaveChanges();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException e)
                {
                    foreach (var eErr in e.EntityValidationErrors)
                    {
                        // Write log to Output
                        System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                            eErr.Entry.Entity.GetType().Name, eErr.Entry.State);
                        foreach (var vErr in eErr.ValidationErrors)
                        {
                            System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                vErr.PropertyName, vErr.ErrorMessage);
                        }

                        /*
                        // Write log to Console
                        System.Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                            eErr.Entry.Entity.GetType().Name, eErr.Entry.State);
                        foreach (var vErr in eErr.ValidationErrors)
                        {
                            System.Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                vErr.PropertyName, vErr.ErrorMessage);
                        }
                        */
                    }
                    throw;
                }