来自 EFCore.BulkExtensions 的 BulkInsertAsync 似乎没有插入任何内容

BulkInsertAsync from EFCore.BulkExtensions doesn't seem to insert anything

我正在使用 EFCore.BulkExtensions 将最多 100,000 条记录批量插入数据库。

我的问题是,出于某种原因,BulkInsertAsync 没有向数据库中插入任何记录。我知道这是一个异步调用,但是我等了半个小时,没有插入任何数据。常规的同步 BulkInsert 调用可以工作,但不是最理想的,因为它占用了大量的处理时间。

有谁知道为什么 BulkInsertAsync 在这种情况下不起作用,而 BulkInsert 却起作用?

这是相关代码,它将二进制数据(从另一台机器传输)编组到 C 结构中,将该 C 结构添加到列表中,然后将该列表批量插入到数据库中。

            IList<object> records = new List<object>();
            using (var db = new RecordContext())
            {
                // keep going until we process all the data; numRecs is at most 100,000
                for (int i = 0; i < numRecs; i++)
                {
                    // marshal the data into the struct and map it to its database struct
                    var tempStruct = Marshal.PtrToStructure<T>(dataPtr);
                    records.Add(tempStruct);
                    dataPtr += Marshal.SizeOf<T>();
                }
                db.BulkInsertAsync(records);
            }

这里是相关的上下文代码:

    public class RecordContext : DbContext
    {
        public RecordContext() : base(GetOptions()) 
        { 
            // disable change tracking for performance
            ChangeTracker.AutoDetectChangesEnabled = false;
        }

        private static DbContextOptions GetOptions()
        {
            return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), new Settings().recordsConnectionString).Options;
        }
        // . . .
    }

从设置中传入以下连接字符串: Data Source=localhost;Integrated Security=True;TrustServerCertificate=True;ApplicationIntent=ReadWrite;Initial Catalog=Records

该方法应该有所作为 - 如果您等待它。

所以不是: db.BulkInsertAsync(records); 做: await db.BulkInsertAsync(records);

本质上,你告诉它执行一个异步任务,然后你通过不调用 await 退出了创建/拥有任务的方法,导致任务被放弃。异步任务有一些微妙的魔力,大声笑。