I am getting a DbEntityValidationException
while trying to save an object to the database using the following code:
public void Save(Employee emp)
{
repository.Insert(emp);
unitOfWork.SaveChanges();
}
I then tried to throw the details of the exception using the following:
try
{
repository.Insert(emp);
unitOfWork.SaveChanges();
}
catch (DbEntityValidationException ex)
{
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 I get the following error and I can't seem to see System.Data.Entity.Validation
anywhere to add to my reference to clear this error message:
The type or namespace name 'DbEntityValidationException' could not be found (are you missing a using directive or an assembly reference?)
How can I fix this?
UPDATE
I just noticed the runtime version of my EF is 4.030319. Could that be the cause?