I have this piece of code:
ids.ForEach(async id =>
{
var grupo = await db.GrupoServicio.FindAsync(id).ConfigureAwait(true);
if (grupo != null)
{
grupo.GrupoServicioEliminadoEn = DateTime.Now;
n++;
await Command.AgregaGrupoServicioAsync(db, grupo, true).ConfigureAwait(true);
}
});
await db.SaveChangesAsync().ConfigureAwait(true);
The system is throwing an exception at the SaveChangesAsync
call.
The error is:
A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.
As you see, I am using await
keyword.
How can I solve it? Should I replace the ForEach
call by a normal foreach
loop? Or should I wrap the ForEach
call with a Task.Run
?
Regards Jaime