Last post Sep 20, 2018 07:17 AM by Brando ZWZ
Sep 19, 2018 08:34 PM|tvb2727|LINK
Is there a way (say if an error occurs) to get the data that has failed on a DbUpdateException from the context object?
I tried something like the below, but it always has 0 records found:
public override int SaveChanges()
var entities = (from entry in ChangeTracker.Entries()
where entry.State == EntityState.Modified || entry.State == EntityState.Added
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
var modifiedEntries = biContext.ChangeTracker
.Where(x => x.State == EntityState.Modified)
.Select(x => x.Entity)
foreach (var eve in modifiedEntries)
sb.AppendLine("Info: " + eve.ToString());
Sep 20, 2018 07:17 AM|Brando ZWZ|LINK
As far as I know, there are four possible checks that can be applied to data before it is written to the database. Three of these are inside EF and are easy to set up while the last one involved capturing errors reported by the underlying database.
So I suggest you could follow below article to know how to catch the bad data in EF core.
More details, you could refer to below article.