这被证明有点难以追查,但是当使用Effort测试Entity Framework 6时,我似乎在尝试访问一个时遇到KeyNotFoundException(“给定的密钥在字典中不存在”)错误DBSet存储库。
我注意到它在DbContext中使用了一个或两个DbSet,但是一旦我开始向DbContext添加多个DbSets,我就会收到上述错误。
示例代码(这是我整个代码的简化,当我从DbContext中注释掉一些DbSets然后将它们放回去时,错误似乎是随机的。我也在模型上有部分类,但它有时也成功了,所以看起来很奇怪):
测试
[Fact]
public void MyTest()
{
var connection = Effort.DbConnectionFactory.CreateTransient();
var context = new StubDbContext(connection);
var count = context.Models1.Count();
Assert.Equal(count, 0);
}
DBContext和DbSets模型
public class StubEntityModelA
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class StubEntityModelB
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class StubEntityModelC
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class StubDbContext : DbContext
{
public StubDbContext(DbConnection connection): base(connection, true)
{
}
public virtual DbSet<StubEntityModelA> Models1 { get; set; }
public virtual DbSet<StubEntityModelB> Models2 { get; set; }
public virtual DbSet<StubEntityModelC> Models3 { get; set; }
}
堆栈追踪:
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Effort.Provider.EffortProviderManifest.GetStoreType(TypeUsage edmType)
at System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty property, String columnName, Boolean isInstancePropertyOnDerivedType)
at System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EntityType entityType, IEnumerable`1 properties, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList`1 propertyPath, Boolean createNewColumn)
at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping)
at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at XXXX.Business.Test.XXXXTests.IXXXXXXMethod.ShouldInsertRecordWhenNoneAlreadyExist() in C:\Workspaces\XXX\XXXXX\XXXXX.Business.Test\XXXXXXTests.cs:line 125
答案 0 :(得分:1)
我遇到了确切的问题。原来是因为我在我的一些SQL 2014数据库表中使用了SQL地理数据类型,并且努力doesn't support this data type,并且目前还没有计划添加对它的支持,这使我处于因为我无法找到EF6的任何其他内存数据库提供程序,所以确实存在问题!
可能还有其他较新的参考类型字段,它也不支持,不确定。
答案 1 :(得分:0)
我有完全相同的堆栈跟踪,但我遇到的问题与实体框架的继承TPT 有关。如果您也在使用它,请查看我的回答here。