New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DbEntityValidationException #4434
Comments
In EFCore (former EF7), there is no build in entity validation. As a result there is no If you want to keep the validation behavior known in EF6, you have to implement it yourself. However, this is as trivial as applying the following lines of code to public class YourDbContext : DbContext
{
public override int SaveChanges()
{
var entities = (from entry in ChangeTracker.Entries()
where entry.State == EntityState.Modified || entry.State == EntityState.Added
select entry.Entity);
var validationResults = new List<ValidationResult>();
foreach (var entity in entities)
{
if(!Validator.TryValidateObject(entity, new ValidationContext(entity), validationResults))
{
// throw new ValidationException() or do whatever you want
}
}
return base.SaveChanges();
}
} |
thank you @davidroth ... i will give it a shot... |
Where is the |
System.ComponentModel.Annotations |
|
Hi... in EF6 I had the code below that overrides the DbContext SaveChanges method... the code basically catches a DbEntityValidationException and does some manipulation of the error messages it exposes... I am having trouble porting this over to EF7... I don't see DbEntityValidationException is available... any ideas on how to convert this code to EF7???? thank you
The text was updated successfully, but these errors were encountered: