并行DB调用时死锁

时间:2017-05-29 09:20:22

标签: c# asp.net-mvc

我在尝试对DB进行两次并行调用时面临死锁

public async Task<IActionResult> Index(){

 var firstTask = this._uow.GetCountries(); //create task to run it in background

 var secondResult = await _uow.GetStates(); //call method and get result from Task
 var firstResult = await firstTask;  //trying to get result from first task

 return View();
}

工作单元方法很简单,使用实体框架:

 public async Task<Countries> GetCountries()
        {
           return  await dbContext.Countries.ToListAsync(); // 
        }

 public async Task<States> GetStates()
        {
           return  await dbContext.States.ToListAsync();
        }

无论如何调用这样的方法会解决问题,我想知道为什么?

public async Task<IActionResult> Index(){
   var secondResult = await _uow.GetStates(); 
   var firstResult = await _uow.GetCountries();

 return View();
}

但随着这个我失去了主要想法 - 并行呼叫

0 个答案:

没有答案