This project has moved. For the latest updates, please go here.
8
Vote

KeyNotFoundException In MappingAPI: The given key was not present in the dictionary (table has foreign key in another table)

description

I am using EF6 and due to the low speed of AddRange() method I need to use BulkInsert. So I added the NuGet package of BulkInsert for EF6 via here.

First thing I received after adding the dlls was this warning:
Found conflicts between different versions of the same dependent
assembly. Please set the "AutoGenerateBindingRedirects" property to
true in the project file.
I made a List of all my Contact entities namely contactsToInsert that need to be added (My contacts have a foreign key in another table, too). When I tried to run the following code I receive a KeyNotFoundException that claims "The given key was not present in the dictionary".
using (var db = new Entities(myConnectionString))
{
    db.BulkInsert(contactsToInsert);
    db.SaveChanges();
}
NB. I am running the BulkInsert inside a BackgroundWorker. Could this possibly be the cause of the issue judging by this fix?

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 109
   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 Prospect.Update.bw_DoWork(Object sender, DoWorkEventArgs e) in c:\Users\pedram.mobedi\Documents\Visual Studio 2013\Projects\Prospect\Update.cs:line 546
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)


[1]: https://www.nuget.org/packages/EntityFramework.BulkInsert-ef6
[2]: https://github.com/Thorium/EntityFramework.BulkInsert

comments

kblc wrote Oct 1, 2015 at 2:17 PM

I also have this error
                using(var ts = new System.Transactions.TransactionScope())
                try
                {
                    this.Context.BulkInsert(instances);
                    this.SaveChanges(true);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    ...
                    throw e;
                }
Result StackTrace:
в System.Collections.Generic.Dictionary2.get_Item(TKey key)
в EntityFramework.MappingAPI.Mappers.MapperBase.BindForeignKeys() в c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappers\MapperBase.cs:строка 603
в EntityFramework.MappingAPI.Mappings.DbMapping..ctor(DbContext context) в c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Mappings\DbMapping.cs:строка 101
в EntityFramework.MappingAPI.EfMap.Get(DbContext context) в c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\EfMap.cs:строка 60
в EntityFramework.MappingAPI.Extensions.MappingApiExtensions.Db(DbContext ctx, Type type) в c:\dev\EntityFramework.MappingAPI\trunk\src\EntityFramework.MappingAPI\Extensions\MappingApiExtensions.cs:строка 50
в System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer)
в EntityFramework.BulkInsert.Helpers.MappedDataReader
1..ctor(IEnumerable1 enumerable, IEfBulkInsertProvider provider) в c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Helpers\MappedDataReader.cs:строка 58
в EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run[T](IEnumerable
1 entities, SqlTransaction transaction, BulkInsertOptions options) в c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\EfSqlBulkInsertProviderWithMappedDataReader.cs:строка 22
в EntityFramework.BulkInsert.Providers.ProviderBase2.Run[T](IEnumerable1 entities, IDbTransaction transaction, BulkInsertOptions options) в c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:строка 77
в EntityFramework.BulkInsert.Providers.ProviderBase2.Run[T](IEnumerable1 entities, BulkInsertOptions options) в c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:строка 105
в EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable1 entities, SqlBulkCopyOptions sqlBulkCopyOptions, Nullable1 batchSize) в c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:строка 95
в EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable1 entities, Nullable1 batchSize) в c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:строка 75
в RoyaltyRepository.Repository.HostAddBulk(IEnumerable`1 instances) в C:\Users\1\Documents\Visual Studio 2012\Projects\Royalty\RoyaltyRepository\Logic\Host.cs:строка 76
--- End of inner exception stack trace ---
в RoyaltyRepository.Repository.HostAddBulk(IEnumerable`1 instances) в C:\Users\1\Documents\Visual Studio 2012\Projects\Royalty\RoyaltyRepository\Logic\Host.cs:строка 91
в RoyaltyRepositoryTests.RepositoryTest.Host_AddBulk() в C:\Users\1\Documents\Visual Studio 2012\Projects\Royalty\RoyaltyRepositoryTests\RepositoryTest.cs:строка 262
Result Message:
Test method RoyaltyRepositoryTests.RepositoryTest.Host_AddBulk threw exception:
System.Exception: Данный ключ отсутствует в словаре. ---> System.Collections.Generic.KeyNotFoundException: Данный ключ отсутствует в словаре.

I try to download sources for fix error.. but I can't build this :(
Please, fix it

wrote Nov 5, 2015 at 10:15 AM

Yulianna wrote May 13, 2016 at 10:37 AM

Anybody did solve this problem?

wrote Jul 15, 2016 at 12:19 AM

wrote Aug 3, 2016 at 9:34 AM

wrote Jan 16 at 6:14 PM