User-1188570427 posted
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
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();
OR
var modifiedEntries = biContext.ChangeTracker
.Entries()
.Where(x => x.State == EntityState.Modified)
.Select(x => x.Entity)
.ToList();
foreach (var eve in modifiedEntries)
{
sb.AppendLine("Info: " + eve.ToString());
}