So let’s say that I need to update a list of objects.
using(db)
{
repository = new Repository<Publication>(db);
foreach (KeyValuePair<int,int> item in publications)
{
Publication publication = repository.GetById(item.Key);
if (publication != null)
{
publication.Quantity = publication.Quantity - item.Value;
if (publication.Quantity > 0)
db.Publication.Attach(publication);
}
}
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException e)
{
throw new Exception("Could not update the database", e);
}
}
}
When I tried to save all the objects, if someone fails, it should be in the catch block, but my question is: how can I get the specific object that throws the exception?
You’re catching
DbUpdateConcurrencyException, which has anEntriesproperty, documented as:So basically that gives you all the problematic ones.