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();
});
}
source
share