2

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? enter image description here

Yanov Adamsky
  • 122
  • 2
  • 9
  • Possible duplicate of [DbEntityValidationException - How can I easily tell what caused the error?](http://stackoverflow.com/questions/15820505/dbentityvalidationexception-how-can-i-easily-tell-what-caused-the-error) – santosh singh Dec 24 '15 at 17:20
  • Thanks for your quick answer, but I am getting an error with the DbEntityValidationException itself. My question is, how can I resolve the last error message in my post? – Yanov Adamsky Dec 24 '15 at 17:27
  • 1
    Is EF referenced in the assembly where your `Save` method runs? By the way, I think the purpose of repository/UoW is to abstract away data access details, so this code that analyzes validation errors should be inside the UoW and the UoW should throw a custom exception. – Gert Arnold Dec 24 '15 at 21:26
  • Thanks so much @GertArnold for catching this. I placed the code inside the unitOfWork and this got rid of the error message. My problem is solved. Thank you! – Yanov Adamsky Dec 24 '15 at 23:30

2 Answers2

2

In Entity framework 5 , The DbEntityValidationException class is defined in System.Data.Entity.Validation namespace(which is in the EntityFramework dll). You need to include a using statement for this namespace so that classes belongs to this namespace are available in your class.

Add this line to your class and then you should be able to use this exception class.

using System.Data.Entity.Validation;
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I tried to add the using statement and could not find **System.Data.Entity.Validation**. I can only get **System.Data.Entity** – Yanov Adamsky Dec 24 '15 at 17:32
  • Are you sure you are on EF 5 ? I just verified and it is in the EntityFramework.dll – Shyju Dec 24 '15 at 17:48
0

As suggested by Gert Arnold, I placed the code that analyzes the validation errors insite my unitOfWork.SaveChanges method and I no more have the

DbEntityValidationException' could not be found

error. Thanks all!

Yanov Adamsky
  • 122
  • 2
  • 9