Running several EntityFramework database queries in parallel

后端 未结 2 894
栀梦
栀梦 2020-12-31 06:05

I am trying to run 3 database queries in parallel but I\'m not sure that I am doing it correctly.

I have made 3 functions which each make a query to the database.

2条回答
  •  感动是毒
    2020-12-31 06:45

    No they run one after eachother if you call them like that using await foreach method, it will run first method, then the second one...etc. And the last part await Task.WhenAll(), you did not supplied the tasks to wait for completion.

    To run them in parallel you have to do it like this:

    var TaskAccountCode = getAccountCodeAsync(deviceId);
    var TaskDeviceType = getDeviceTypeAsync(deviceId);
    var TaskUsername = getUserNameAsync(userId);
    await Task.WhenAll(TaskAccountCode, TaskDeviceType,TaskUsername);   
    

提交回复
热议问题