-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and improve async documentation
* Merge query and saving async pages * Move under fundamentals * Document ambiguous invocation issues with System.Interactive.Async and show workaround. Closes #1722 Related to #1332 Related to dotnet/efcore#18124
- Loading branch information
Showing
13 changed files
with
136 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: Asynchronous Programming - EF Core | ||
description: Querying and saving data asynchronously with Entity Framework Core | ||
author: roji | ||
ms.date: 9/2/2020 | ||
ms.assetid: 38f71624-7a68-4c72-a918-3e7b858ef090 | ||
uid: core/miscellaneous/async | ||
--- | ||
# Asynchronous Programming | ||
|
||
Asynchronous queries avoid blocking a thread while the query is executed in the database. Async queries are important for keeping a responsive UI in thick-client applications, and can also increase throughput in web applications where they free up the thread to service other requests in web applications. | ||
|
||
Following the .NET standard, EF Core provides asynchronous counterparts to all synchronous methods which perform I/O. These have the same effects as the sync methods, and can be used with the C# `async` and `await` keywords. For example, instead of using DbContext.SaveChanges, which will block a thread while database I/O is performed, DbContext.SaveChangesAsync can be used: | ||
|
||
[!code-csharp[Main](../../../samples/core/Miscellaneous/Async/Program.cs#SaveChangesAsync)] | ||
|
||
For more information,, see [the general C# asynchronous programming docs](/dotnet/csharp/async). | ||
|
||
> [!WARNING] | ||
> EF Core doesn't 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. This is typically done by using the `await` keyword on each async operation. | ||
# Async LINQ operators | ||
|
||
In order to support executing LINQ queries asynchronously, EF Core provides a set of async extension methods which execute the query and return results. These counterparts to the standard, synchronous LINQ operators include ToListAsync, SingleAsync, AsAsyncEnumerable, etc.: | ||
|
||
[!code-csharp[Main](../../../samples/core/Miscellaneous/Async/Program.cs#ToListAsync)] | ||
|
||
Note that there are no async versions of some LINQ operators such as Where or OrderBy, because these only build up the LINQ expression tree and don't cause the query to be executed in the database. Only operators which cause query execution have async counterparts. | ||
|
||
> [!IMPORTANT] | ||
> The EF Core async extension methods are defined in the `Microsoft.EntityFrameworkCore` namespace. This namespace must be imported for the methods to be available. | ||
# Client-side async LINQ operators | ||
|
||
The async LINQ operators discussed above can only be used on EF queries - you cannot use them with client-side LINQ to Objects query. To perform client-side async LINQ operations outside of EF, use the [System.Interactive.Async package](https://www.nuget.org/packages/System.Interactive.Async); this can be especially useful for performing operations on the client that cannot be translated for evaluation at the server. | ||
|
||
Unfortunately, referencing System.Interactive.Async causes ambiguous invocation compilation errors on LINQ operators applied to EF's DbSets; this makes it hard to use both EF and System.Interactive.Async in the same project. To work around this issue, add AsQueryable to your DbSet: | ||
|
||
```c# | ||
var groupedHighlyRatedBlogs = await context.Blogs | ||
.AsQueryable() | ||
.Where(b => b.Rating > 3) // server-evaluated | ||
.AsAsyncEnumerable() | ||
.GroupBy(b => b.Rating) // client-evaluated | ||
.ToListAsync(); | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<RootNamespace>EFAsync</RootNamespace> | ||
<AssemblyName>EFAsync</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace EFAsync | ||
{ | ||
public class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
await using var context = new BloggingContext(); | ||
await context.Database.EnsureDeletedAsync(); | ||
await context.Database.EnsureCreatedAsync(); | ||
|
||
#region SaveChangesAsync | ||
var blog = new Blog { Url = "http://sample.com" }; | ||
context.Blogs.Add(blog); | ||
await context.SaveChangesAsync(); | ||
#endregion | ||
|
||
#region ToListAsync | ||
var blogs = await context.Blogs.Where(b => b.Rating > 3).ToListAsync(); | ||
#endregion | ||
} | ||
} | ||
|
||
public class BloggingContext : DbContext | ||
{ | ||
public DbSet<Blog> Blogs { get; set; } | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFAsync;Trusted_Connection=True;ConnectRetryCount=0"); | ||
} | ||
} | ||
|
||
public class Blog | ||
{ | ||
public int BlogId { get; set; } | ||
public string Url { get; set; } | ||
public int Rating { get; set; } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.