非同步查詢Asynchronous Queries

非同步查詢可避免在資料庫中執行查詢時封鎖執行緒。Asynchronous queries avoid blocking a thread while the query is executed in the database. 若要避免凍結大型用戶端應用程式的 UI,這非常實用。This can be useful to avoid freezing the UI of a thick-client application. 非同步作業也可以提高 Web 應用程式中的輸送量,可在資料庫作業完成時釋放執行緒,以便為其他要求提供服務。Asynchronous operations can also increase throughput in a web application, where the thread can be freed up to service other requests while the database operation completes. 如需詳細資訊,請參閱 使用 C# 進行非同步程式設計For more information, see Asynchronous Programming in C#.

警告

EF Core 不支援在同一個內容執行個體上執行多個平行作業。EF Core does not support multiple parallel operations being run on the same context instance. 您一律應等候作業完成,然後再開始下一項作業。You should always wait for an operation to complete before beginning the next operation. 這通常會在每個非同步作業上使用 await 關鍵字來完成。This is typically done by using the await keyword on each asynchronous operation.

Entity Framework Core 提供一組非同步擴充方法,這組方法可用來作為會導致執行查詢並傳回結果的 LINQ 方法替代項目。Entity Framework Core provides a set of asynchronous extension methods that can be used as an alternative to the LINQ methods that cause a query to be executed and results returned. 範例包括 ToListAsync()ToArrayAsync()SingleAsync() 等。沒有 LINQ 運算子的非同步版本 (例如 Where(...)OrderBy(...) 等),因為這些方法只會逐漸形成 LINQ 運算式樹狀架構,而不會導致查詢在資料庫中執行。Examples include ToListAsync(), ToArrayAsync(), SingleAsync(), etc. There are not async versions of LINQ operators such as Where(...), OrderBy(...), etc. because these methods only build up the LINQ expression tree and do not cause the query to be executed in the database.

重要

EF Core 非同步擴充方法定義於 Microsoft.EntityFrameworkCore 命名空間中。The EF Core async extension methods are defined in the Microsoft.EntityFrameworkCore namespace. 您必須先匯入此命名空間,才能使用方法。This namespace must be imported for the methods to be available.

public async Task<List<Blog>> GetBlogsAsync()
{
    using (var context = new BloggingContext())
    {
        return await context.Blogs.ToListAsync();
    }
}