How to handle System.Data.Entity.Validation.DbEntityValidationException?

There is some sort of database validation happening preventing you from writing the data into it.

The solution is already stated on this page:

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

As an extra note to this as you are using .net mvc you should use System.Diagnostics.Debug.WriteLine() instead of Console.Writeline() and this will write to the debug output window when you are debugging. As you cannot write to the console when running a mvc project.


Even though there is an accepted answer already, my experience could probably help someone in the future. For a quick test you can check the data which you are inputting to the database in Configuration.cs file and then in Model you can check the validation conditions. For example, in my case I would put following validation condition in a model:

[Range(1, 100),DataType(DataType.Currency)]
public decimal Price { get; set; }

And then, inside the Configuration.cs assigning the price to be:

new Photo{
    Title = "Photo 2",
    DateTaken = DateTime.Parse("2013-6-15"),
    Genre = "Nature",
    CameraModel = "Canon",
    Price = 200
}

This, created EntityFrameworkExceptions and prevented database from seeding.


To solve this error, we can wrap the SaveChanges() method of DatabaseContext object in try block and in the Catch loop through each errors to find out where the error is. The code goes below.

try
{
    db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
    foreach (var entityValidationErrors in ex.EntityValidationErrors)
    {
        foreach (var validationError in entityValidationErrors.ValidationErrors)
        {
            Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
        }
    }
}

Once the error is found, you can work on that to fix it. Hope this helps.


You can override the SaveChanges, to handle this exception and provide better exception details.

You can create a class "next" to your context class... the full code for that class is as follow:

using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;

namespace MyNamespace
    {
        public partial class MyContext : DbContext
        {
            // Override base SaveChanges to expand out validation errors so client gets an actually helpful message
            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);

                    // Join the list to a single string.
                    var fullErrorMessage = string.Join("; ", errorMessages);

                    // Combine the original exception message with the new one.
                    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);
                }
            }
        }
    }

Check this for more information: http://devillers.nl/blog/improving-dbentityvalidationexception/