簡體   English   中英

使 IEnumerable Entity Framework 結果異步 Web API

[英]Make IEnumerable Entity Framework result async Web API

我有一個 Web API(not.core web API)方法使用實體框架從數據庫中返回一些記錄。 結果是 IEnumerable Model 我想修改它以使用 Async/Await 技術來提高性能,但我不知道如何! 我嘗試使用ToListAsync但它不起作用。

我的Web API方法代碼

[HttpGet]
[Route("api/student/{id}/")]
public IEnumerable<Student> Get(int id)
{            
    try
    {
        var db = new DBEntities();
        return db.GetStudentDetails(id);
    }
    catch(Exception exception)
    {
        _logger.Error(exception.Message);
        throw new HttpResponseException(HttpStatusCode.ExpectationFailed);
    }
}

我的實體框架 function 代碼:

public virtual ObjectResult<Student> GetStudentDetails(int id)
{
     var idParameter = id.HasValue ?
            new ObjectParameter("id", id) :
            new ObjectParameter("id", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Student>("sp_get_student_details", idParameter);
 }

提前致謝。

這應該可以解決問題

public virtual async Task<ObjectResult<Student>> GetStudentDetails(int id)
{
      var idParameter = id.HasValue ?
      new ObjectParameter("id", id) :
      new ObjectParameter("id", typeof(int));

      return await ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Student>("sp_get_student_details", idParameter);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM