批量插入错误实体框架

时间:2016-06-29 15:54:36

标签: c# .net entity-framework

enter image description here 批量插入时出现此错误。仅适用于大量记录。

switch (actionType)
            {
                case AuditLogSaveAction.Asynchronous:
                    ORMAsyncActionQueue.AuditLog.AddLogsToQueue(logsToSave);
                    break;
                case AuditLogSaveAction.Synchronous:
                    **this.BulkInsert<AuditLog>(logsToSave); <- Exception Thrown here.**
                    break;
            }

1 个答案:

答案 0 :(得分:1)

我有同样的error。如果您将BulkInset与一些并发逻辑一起使用,例如:多个线程或任务,您可以完全接受此错误。它导致不是线程安全的代码(在本机存储库中已修复),但相应的Nuget Package仍然存在此错误,因此您可以轻松解决问题 - 只需下载存储库并用它替换当前的Nuget包。 This peace of code contains exception

public static IEfBulkInsertProvider Get(DbContext context)
{
    var connectionTypeName = context.Database.Connection.GetType().FullName;
    if (!Providers.ContainsKey(connectionTypeName))
    {
        throw new BulkInsertProviderNotFoundException(connectionTypeName);
    }

    return Providers[connectionTypeName]().SetContext(context);
}

不是Providers属性的线程安全代码,如上所示:

private static Dictionary<string, Func<IEfBulkInsertProvider>> Providers
{
    get
    {
        //commented pice of code does not exist at Nuget Package
        //lock (ProviderInitializerLockObject)
        //{
            if (_providers == null)
            {
                _providers = new Dictionary<string, Func<IEfBulkInsertProvider>>();

                // bundled providers
                Register<EfSqlBulkInsertProviderWithMappedDataReader>("System.Data.SqlClient.SqlConnection");
                //Register<EfSqlCeBulkiInsertProvider>("System.Data.SqlServerCe.4.0");
            }
        //}

        return _providers;
    }
}