Let's say I have an Entity which has a concurrency token column configured in EF core. When an exception occours because data the changed in the database, I'd like to retry the whole transaction from a clean context.
All of the examples use the following pattern:
using (var context = new PersonContext())
{
try
{
//Business logic
}
catch (DbUpdateConcurrencyException ex)
{
//Reload/merge entries in ex.Entries, etc...
}
}
The example is working, but how do you handle this scenario when the DbContext is registered as a scoped service, and it's injected into the repositories and you have a more complex scenario. I think it would be easier to retry the whole business process than handling the conflicts.
public class SomeService {
public SomeService (ISomeRepository repo)
{
}
public Task SomeMethod()
{
try
{
//Business logic
}
catch (DbUpdateConcurrencyException ex)
{
//clean context, restart SomeMethod()
}
}
}
What would be a clean way to handle the retry process?