How can I easily tell what caused the error – DbEntityValidationException ?

I came across with the situation where I have used Entity Framework 6.0. While calling SaveChanges on my DbContext, I get the following exception:

System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

 

After going into the exception detail all I could get to know that there is an exception at SaveChanges() method. So this is what I have done:

System.Data.Entity.Validation.DbEntityValidationException was unhandled
 HResult=-2146232032
 Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
 Source=EntityFramework
 StackTrace:
 at System.Data.Entity.Internal.InternalContext.SaveChanges()
 at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
 at System.Data.Entity.DbContext.SaveChanges()
 at ConsoleApplication1.Program.Main() in c:UsersamitmSkyDrivecodeConsoleApplication1ConsoleApplication1Program.cs:line 19
 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
 at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
 at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
 at System.Threading.ThreadHelper.ThreadStart()
 InnerException:

No where it is telling me about why it has actually occured.

 

Solution:

To solve this problem what I did is I override SaveChanges method, catch the exception of type DbEntityValidationException then get an entire exception through Linq:

public partial class BookContainer : DbContext
 {
 public BookContainer()
 : base("name=BookContainer")
 {
 }
public override int SaveChanges()
 {
 try
 {
 return base.SaveChanges();
 }
 catch (DbEntityValidationException ex)
 {
 // Retrieve the error messages as a list of strings.
 var errorMessages = ex.EntityValidationErrors
 .SelectMany(x => x.ValidationErrors)
 .Select(x => x.ErrorMessage);
 var fullErrorMessage = string.Join("; ", errorMessages);
 var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
// Throw a new DbEntityValidationException with the improved exception message.
 throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
 }
 }

 

Now when I get the exception result, I get it with “Column name” that has caused this trouble.


System.Data.Entity.Validation.DbEntityValidationException was unhandled
 HResult=-2146232032
 Message=Validation failed for one or more entities.
See 'EntityValidationErrors' property for more details.
The validation errors are: The WebSite field is required.;
The WebSite field is required.;
The WebSite field is required.; The WebSite field is required.
 Source=ConsoleApplication1
 StackTrace:
 at ConsoleApplication1.BookContainer.SaveChanges() in c:UsersamitmSkyDrivecodeConsoleApplication1ConsoleApplication1Book.Context.cs:line 45
 at ConsoleApplication1.Program.Main() in c:UsersamitmSkyDrivecodeConsoleApplication1ConsoleApplication1Program.cs:line 19
 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
 at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
 at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
 at System.Threading.ThreadHelper.ThreadStart()
 InnerException: