Entity Framework 6.1 : The given key was not present in the dictionary
Asked Answered
E

3

2

I have a table with some relations, the program works fine until I add a new relation between this table and customer table, the ddl for PermissionCode Table (first table) is as below:

CREATE TABLE [dbo].[PermissionCode] (
[Id] int NOT NULL IDENTITY(1,1) ,
[Salt] varchar(3) COLLATE Turkish_CI_AS NOT NULL ,
[Code] nvarchar(12) COLLATE Turkish_CI_AS NOT NULL ,
[StartDate] date NULL ,
[EndDate] date NULL ,
[TypeId] int NOT NULL ,
[UserId] nvarchar(128) COLLATE Turkish_CI_AS NULL ,
[OwnerId] int NULL ,
[CategoryId] int NULL ,
[IsActive] bit NOT NULL DEFAULT ((1)) ,
[InsertIdentifier] varchar(8) COLLATE Turkish_CI_AS NULL ,
[IsForTeacher] bit NOT NULL DEFAULT ((0)) ,
CONSTRAINT [PK__Table__3214EC0759063A47] PRIMARY KEY ([Id]),
CONSTRAINT [FK_PermissionCode_Category] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Category] ([Id]) ON DELETE SET NULL ON UPDATE NO ACTION,
CONSTRAINT [FK_PermissionCode_Customer] FOREIGN KEY ([OwnerId]) REFERENCES [dbo].[Customers] ([Id]) ON DELETE SET NULL ON UPDATE NO ACTION,
CONSTRAINT [FK_PermissionCode_PremissionCodeType] FOREIGN KEY ([TypeId]) REFERENCES [dbo].[Category] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT [FK_PermissionCode_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT [UQ__Permissi__DB7779646E01572D] UNIQUE ([Salt] ASC, [Code] ASC)
)
ON [PRIMARY]
GO   

CREATE INDEX [IX_PermissionCode_Code ] ON [dbo].[PermissionCode]
([Code] ASC) 
ON [PRIMARY]
GO

and the ddl for my customer table is as below:

CREATE TABLE [dbo].[Customers] (
[Id] int NOT NULL IDENTITY(1,1) ,
[UserId] nvarchar(128) COLLATE Turkish_CI_AS NOT NULL ,
[OfficeName] nvarchar(200) COLLATE Turkish_CI_AS NULL ,
[CityId] int NULL ,
[StateId] int NULL ,
[Address] nvarchar(1000) COLLATE Turkish_CI_AS NULL ,
[Tel1] nvarchar(20) COLLATE Turkish_CI_AS NULL ,
[Tel2] nvarchar(20) COLLATE Turkish_CI_AS NULL ,
[Fax] nvarchar(20) COLLATE Turkish_CI_AS NULL ,
[ResponsiblePersonFirstName] nvarchar(50) COLLATE Turkish_CI_AS NULL ,
[ResponsiblePersonLastName] nvarchar(100) COLLATE Turkish_CI_AS NULL ,
[Deleted] bit NOT NULL DEFAULT ((0)) ,
[CustomerType] varchar(10) COLLATE Turkish_CI_AS NOT NULL ,
CONSTRAINT [PK__Customer__3214EC07345EC57D] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Customers_City] FOREIGN KEY ([CityId]) REFERENCES [dbo].[City] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT [FK_Customers_State] FOREIGN KEY ([StateId]) REFERENCES [dbo].[State] ([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT [FK_Customers_Users] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE ON UPDATE CASCADE
)
ON [PRIMARY]
GO

I use Entityframework.bulkinsert plugin to bulk insert permission codes for a customer. If I remove FK_PermissionCode_Customer foreign key the program runs without error, and insert permission codes successfully to db, but when adding this fk relationship entity framework issue this error:

at System.Collections.Generic.Dictionary2.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](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector) at EntityFramework.BulkInsert.Helpers.MappedDataReader1..ctor(IEnumerable1 enumerable, IEfBulkInsertProvider provider) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Helpers\MappedDataReader.cs:line 58 at EntityFramework.BulkInsert.Providers.EfSqlBulkInsertProviderWithMappedDataReader.Run[T](IEnumerable1 entities, SqlTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\EfSqlBulkInsertProviderWithMappedDataReader.cs:line 22 at EntityFramework.BulkInsert.Providers.ProviderBase2.Run[T](IEnumerable1 entities, IDbTransaction transaction, BulkInsertOptions options) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Providers\ProviderBase.cs:line 77 at EntityFramework.BulkInsert.Providers.ProviderBase2.Run[T](IEnumerable1 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, IEnumerable1 entities, SqlBulkCopyOptions sqlBulkCopyOptions, Nullable1 batchSize) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:line 95 at EntityFramework.BulkInsert.Extensions.BulkInsertExtension.BulkInsert[T](DbContext context, IEnumerable1 entities, Nullable1 batchSize) in c:\dev\EntityFramework.BulkInsert\dev\Src\EntityFramework.BulkInsert\Extensions\BulkInsertExtension.cs:line 75 at xxx.Domain.Repositories.PermissionCodeRepository.InsertRange(IList1 permissionCodes) in xxx.Domain\Repositories\PermissionCodeRepository.cs:line 92 at xxx.Admin.Controllers.PermissionCodeController.Create(CreatePermissionCodeViewModel model) in xxx.WebUI.Admin\Controllers\PermissionCodeController.cs:line 169

I can't found the problem; for testing purpose, I change my db first to code first approach and problem solved. but I want to now the reason for this issue, is this a bug? or am I need do one more step in entity framework model designer?

Eveleen answered 17/10, 2014 at 13:10 Comment(1)
@Gert Arnold: the error is given in title, there was no more than this string: "The Given key was not present in the dictionary."Eveleen
H
1

It's a bug. It happened because you have rename your pocos and/or dbsetnames.

Retry dbfirst without rename and you'll certainly bulk it!

Hayott answered 17/10, 2014 at 14:27 Comment(4)
Could you explain more? what you mean from renaiming pocos, I completly delete edmx file, and recreate it but still not workEveleen
Bulk uses EntityFramework.MappingAPI to map pocos. It uses the SSPace method to retrieve table names in your metadataworkspace (blog.cincura.net/230583-metadataworkspace-in-entity-framework). The problem is that MappingApi doesn't found the table in the metadataworkspace but doesn't launch any error.So it breaks in bindingforeignkeys beacause it doesn't have any foreign keys => it doesn't have the table. If this is not your problem, try debugging the dll ;)Hayott
I haven't renamed anything - what would be the cause of this issue? It works doing it normally (but slow!).Pregnable
I am running DB First and I haven't renamed anything. But I still cannot get to run BulkInsert. Here is my question: #32225683 Would appreciate any help because AddRange is extremely slow.Wychelm
A
1

This happened to me in the following situation:

  • DB first
  • no manually renamed entities after updating the model from the database.
  • a table called Users
  • 'Pluralize or singularize generated object names' enabled

I resolved it by renaming the table to User.

NB: I dug a little with .Net Reflector to get to a solution and I assume that the EntityFramework.MappingAPI is trying to find the table User, because the the POCO is called User, although didn't dig further once I had it fixed.

Analisaanalise answered 2/9, 2015 at 16:3 Comment(0)
F
0

For me the error was caused by the Column attribute used on a random entity. E.g.:

[Column("LanguageName")]

public string Name { get; set; }

It is clearly a bug in the BulkInsert columns mapping.

As a side note: the developer that didn't check the key in that dictionary should feel bad. He has wasted a lot of time for us.

Happy codding.

Fronia answered 22/6, 2016 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.