Entity Framework Core - Async / Sync - DbContext

async-await c# entity-component-system entity-framework-core


I come to you for some advice. I'm developing a console application (server) requiring an external library.

I use Entity Framework Core. But I still have gaps regarding the concept of async / await. I have read quite a few things on it, tried quite a few things.

I set up an Entity / DAL / BLL architecture concerning the database and the ECS (Entity Component System) architecture.

I have systems that call an event (OnInitialized). In this event, I need to load different data from database (depending on the system).

It's not possible to make several parallel queries simultaneously on same DbContext. I have only one database with multiple tables.

So should I develop interaction with the database fully sync?

Or is there a pattern to use multiple async in same time?

1/30/2020 3:54:07 PM

Popular Answer

The async/await pattern is only indirectly connected to parallelism.

I think you first need to understand exactly what is happening, when you are "await"ing a Task to return. The await-keyword is a bit misleading in my opinion. It does not really wait for the function to return, instead it starts the function that you called in a separate thread, saves the current thread state in a state-engine and causes the program execution path to return control to the caller. Once the called function is finished, that state-engine reconstructs the threadstate and continues execution.

Parallelizing Tasks is actually pretty simple. Instead of a "await"ing for a function to return, you just grab the task-objects that the functions are returning, collect them in a Collection and call Task.WaitAll(collection). After that, all task-objects will have a result-property containing their results.

1/30/2020 3:40:11 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow