Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EF Core - A way to get the data that is updating on SaveChangesAsync()

$
0
0

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());
                }


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>