EF + async = Context cannot be used during model creation?

The setup is pretty standard. Created a view, as well as a repository with a SQL Server connection string. The view model and view model constructor are passed to the repository. I call the collection from the repository. Everything worked fine until I tried to do this asynchronously using the async / wait combination, but now I get the error “Context cannot be used while creating the model” when I call the repository collection.

Old working code:

void FillPeopleList()
{
  PeopleList = _repository.GetPeople();
}

New broken code:

async void FillPeopleList()
{
  await Task.Run(()=>
  {
    PeopleList = _repository.GetPeople(); // Error
  });
}
+3
source share
1 answer

EF is not thread safe.

You cannot use the same context for multiple threads.

+9
source

All Articles