Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ForEachAsync <t>()的意外行为

时间:2018-06-19 04:25:50

标签: c# entity-framework-core ef-core-2.0 ef-core-2.1

以下是重现的步骤。下面的程序使用.Net Core控制台应用程序和EF Core从一个SQL表复制10,000行到另一个SQL表。该程序以100个批次插入记录,(这很重要!)它为每个插入创建一个新的DbContext实例。

1)创建SQL Server数据库,以及“Froms”和“Tos”表:

create table Froms (
    Id int identity(1, 1) not null,
    Guid [uniqueidentifier] not null,

    constraint [PK_Froms] primary key clustered (Id asc)
)
go

create table Tos (
    Id int not null,
    Guid [uniqueidentifier] not null,

    constraint [PK_Tos] primary key clustered (Id asc)
)
go

2)填充“Froms”表:

set nocount on
declare @i int = 0

while @i < 10000
begin
    insert Froms (Guid)
    values (newid())

    set @i += 1
end
go

3)创建名为TestForEachAsync的.Net Core控制台应用项目。将C#的版本更改为7.1或更高版本(async Main所需)。添加Microsoft.EntityFrameworkCore.SqlServer nuget包。

4)创建课程:

数据库实体

using System;

namespace TestForEachAsync
{
    public class From
    {
        public int Id { get; set; }
        public Guid Guid { get; set; }
    }
}
using System;

namespace TestForEachAsync
{
    public class To
    {
        public int Id { get; set; }
        public Guid Guid { get; set; }
    }
}

的DbContext

using Microsoft.EntityFrameworkCore;

namespace TestForEachAsync
{
    public class Context : DbContext
    {
        public DbSet<From> Froms { get; set; }
        public DbSet<To> Tos { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("YOUR_CONNECTION_STRING");
        }
    }
}

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace TestForEachAsync
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            //Get the "froms"
            var selectContext = new Context();
            var froms = selectContext.Froms.Select(f => new { f.Id, f.Guid });

            int count = 0;
            Task<int> saveChangesTask = null;
            Context insertContext = new Context();
            Context prevInsertContext = null;

            //Iterate through "froms"
            await froms.ForEachAsync(
                async f =>
                {
                    //Add instace of "to" to the context
                    var to = new To { Id = f.Id, Guid = f.Guid };
                    await insertContext.Tos.AddAsync(to);
                    count++;

                    //If another 100 of "to"s has been added to the context...
                    if (count % 100 == 0)
                    {
                        //Wait for the previous 100 "to"s to finish saving to the database
                        if (saveChangesTask != null)
                        {
                            await saveChangesTask;
                        }

                        //Start saving the next 100 "to"s
                        saveChangesTask = insertContext.SaveChangesAsync();

                        //Dispose of the context that was used to save previous 100 "to"s
                        prevInsertContext?.Dispose();

                        //Reassign the context used to save the current 100 "to"s to a "prev" variable,
                        //and set context variable to the new Context instance.
                        prevInsertContext = insertContext;
                        insertContext = new Context();
                    }
                }
            );

            //Wait for second last 100 "to"s to finish saving to the database
            if (saveChangesTask != null)
            {
                await saveChangesTask;
            }

            //Save the last 100 "to"s to the database
            await insertContext.SaveChangesAsync();
            insertContext.Dispose();

            Console.WriteLine("Done");
            Console.ReadKey();
        }
    }
}

5)运行应用程序 - 您获得例外The connection does not support MultipleActiveResultSets。看起来insertContext正在启动多个操作,但我不明白为什么。

6)我找到了两种解决问题的方法:

  • await froms.ForEachAsync(...)循环替换为“普通”循环foreach (var f in froms) {...}
  • 在异步循环内,将await saveChangesTask;替换为saveChangesTask.Wait();

但是,有人可以解释为什么原始代码不能像我期望的那样工作吗?

注意:如果您多次运行应用程序,请不要忘记在每次运行之前截断“Tos”表。

1 个答案:

答案 0 :(得分:3)

您陷入典型的陷阱,即将异步lambda传递给期望委托返回void(在这种情况下为Action<T>)的方法,如Stephen ToubPotential pitfalls to avoid when passing around async lambdas中所述。这实际上等同于使用async void的陷阱,因为您的异步代码根本不是await版,因此破坏了其内部逻辑。

该解决方案照常是特殊的重载,它接受Func<T, Task>而不是Action<T>。可能它应该由EF Core提供(您可以考虑发布请求),但是现在您可以自己使用以下方法实现它:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Extensions.Internal;

namespace Microsoft.EntityFrameworkCore
{
    public static class AsyncExtensions
    {
        public static Task ForEachAsync<T>(this IQueryable<T> source, Func<T, Task> action, CancellationToken cancellationToken = default) =>
            source.AsAsyncEnumerable().ForEachAsync(action, cancellationToken);

        public static async Task ForEachAsync<T>(this IAsyncEnumerable<T> source, Func<T, Task> action, CancellationToken cancellationToken = default)
        {
            using (var asyncEnumerator = source.GetEnumerator())
                while (await asyncEnumerator.MoveNext(cancellationToken))
                    await action(asyncEnumerator.Current);
        }
    }
}

基本上是EF Core implementation,其中await中添加了action

执行完此操作后,您的代码将解析为该方法,并且一切都会按预期工作。