EntityFramework.BulkInsert抛出KeyNotFoundException

时间:2015-11-16 19:49:36

标签: c# entity-framework bulkinsert

我试图使用Entity Framework 6的批量插入扩展。但是,当我调用" BulkInsert"时,我似乎得到了一个KeyNotFoundException。方法

以下是相关代码:

        using (var context = new SupportContext(request.CustomerId))
        {
            using (var transactionScope = new TransactionScope())
            {
                var freeIds =
                    Enumerable.Range(1, 89999)
                        .Except(
                            context.TagAssociationTypes.Where(n => n.TagAssociationTypeID <= 89999)
                                .Select(n => n.TagAssociationTypeID))
                        .ToList();
                var nfeList = context.NumericalFunctionEnums.ToList();
                var processedTats = new List<TagAssociationType>();
                foreach (var item in request.Tats)
                {
                    var groupingNFE = nfeList.FirstOrDefault(n => n.nfe_description == item.GroupingNFE.Value);
                    var summaryNFE = nfeList.FirstOrDefault(n => n.nfe_description == item.SummaryNFE.Value);
                    var tatId = freeIds.Min();

                    var newItem = new TagAssociationType
                    {
                        TagAssociationTypeID = tatId,
                        Name = item.TagAssociationTypeName.Value,
                        GroupingFunctionID = groupingNFE != null ? groupingNFE.nfe_id : 0,
                        SummaryFunctionID = summaryNFE!= null ? summaryNFE.nfe_id : 0
                    };

                    processedTats.Add(newItem);
                    freeIds.Remove(tatId);
                }
                context.BulkInsert(processedTats);
                transactionScope.Complete();
            }
        }

StackTrace

System.Collections.Generic.KeyNotFoundException was unhandled by user code
  HResult=-2146232969
  Message=The given key was not present in the dictionary.
  Source=mscorlib
  StackTrace:
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at EntityFramework.MappingAPI.Mappers.MapperBase.BindForeignKeys() in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\MapperBase.cs:line 603
       at EntityFramework.MappingAPI.Mappings.DbMapping..ctor(DbContext context) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappings\DbMapping.cs:line 101
       at EntityFramework.MappingAPI.EfMap.Get(DbContext context) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\EfMap.cs:line 60
       at EntityFramework.MappingAPI.Extensions.MappingApiExtensions.Db(DbContext ctx, Type type) in c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Extensions\MappingApiExtensions.cs:line 51
       at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
       at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
       at EntityFramework.BulkInsert.Helpers.MappedDataReader`1..ctor(IEnumerable`1 enumerable, IEfBulkInsertProvider provider) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Helpers\MappedDataReader.cs:line 58
       at EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run[T](IEnumerable`1 entities, SqlTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\EfSqlBulkInsertProviderWithMappedDataReader.cs:line 22
       at EntityFramework.BulkInsert.Providers.ProviderBase`2.Run[T](IEnumerable`1 entities, IDbTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 77
       at EntityFramework.BulkInsert.Providers.ProviderBase`2.Run[T](IEnumerable`1 entities, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 105
       at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable`1 entities, SqlBulkCopyOptions sqlBulkCopyOptions, Nullable`1 batchSize) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:line 95
       at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable`1 entities, Nullable`1 batchSize) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:line 75
       at SupportCenter.Controllers.Api.BulkLocalTatController.InsertTats(VerifyQuery request)
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
  InnerException: 

我并不完全确定是什么原因造成的。

1 个答案:

答案 0 :(得分:0)

因此,在深入研究之后,我发现它与MappingAPI有关,它试图映射模型上的所有虚拟属性。为了解决这个问题,我创建了一些特殊模型来去除所有虚拟属性,并且只包含插入所需的属性。

从那里,我专门为我想要插入的模型创建了一个单独的上下文。我没有在上下文中包含任何映射。

这种方法看起来效果很好,但有几个缺点。对于可能跨越许多不同模型的复杂对象,我可能需要使用两个单独的数据库上下文。您还必须将上下文彼此分开,而using语句块中没有重叠,否则会导致TransactionScope抛出错误。