My Entity Framework 6 domain uses attributes and the IValidatableObject to additional rules. However some constraints requires access to the database. As I have dozens of entities the DbContext.ValidateEntity looks a mess with lots of calls to validate each entity.
In the other hand the DbContext.ValidateEntity will not validate the entity on the model binding as the IValidatableObject. So if I use some code like this I will just send back to the user the attributes and IValidatableObject validations:
if (ModelState.IsValid) // Looks for attribute validations and also IValidatableObject
{
db.Foo.Add(foo);
db.SaveChanges(); // DbContext.ValidateEntity triggers only here.
return RedirectToAction("Index");
}
return View(foo);
I found lots of workarounds like use the DbContext inside the IValidatableObject implementation but it breaks the separation of validation scopes.