I want to reload certain entities from db, due to the long operation I want to reload them using async/await. Here is my code:
private async void btnUpdate_Click(object sender, EventArgs e)
{
Task allTasks = UpdateAppointments();
await allTasks;
}
internal async Task UpdateAppointments()
{
IEnumerable<Entities.Apointment> apps = PContext.Appointments.Select(p => p).Where(p => p.Start >= Start && p.End <= End && p.MachineId == 66);
List<Task> task = new List<Task>();
foreach (Entities.Apointment app in apps)
{
task.Add(PContext.Entry(app).ReloadAsync());
}
await Task.WhenAll(task);
}
I get the error:
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.**
Exception details:
{System.NotSupportedException: 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. at System.Data.Entity.Internal.ThrowingMonitor.EnsureNotEntered() at System.Data.Entity.Core.Objects.ObjectContext.RefreshAsync(RefreshMode refreshMode, Object entity, CancellationToken cancellationToken) at System.Data.Entity.Internal.InternalEntityEntry.ReloadAsync(CancellationToken cancellationToken) at System.Data.Entity.Infrastructure.DbEntityEntry`1.ReloadAsync() at PlanningModule.PlanningForm.d__20.MoveNext() in C:\Git\planningmodule\PlanningModule\PlanningForm.cs:line 307 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at PlanningModule.PlanningForm.d__19.MoveNext() in C:\Git\planningmodule\PlanningModule\PlanningForm.cs:line 264}
Inner exception:
null
Stack trace:
_stackTrace {sbyte[192]} object {sbyte[]}
Anys ideas, how to reload entities async?