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

南楼画角 提交于 2019-12-10 10:44:42

问题


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?


回答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!




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/26425789/entity-framework-6-1-the-given-key-was-not-present-in-the-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!