訪問DbSet時的Effort(EF6)異常(字典中沒有給定的密鑰)

.net effort entity-framework entity-framework-6 linq

這被證明有點難以追查,但是當使用Effort測試Entity Framework 6時,我似乎在嘗試訪問其中一個DBSet時收到KeyNotFoundException(“給定的密鑰在字典中不存在”)錯誤庫。

我注意到它在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

熱門答案

我遇到了確切的問題。原來是因為我在我的一些SQL 2014數據庫表中使用了SQL地理數據類型,並且努力不支持這種數據類型 ,並且目前還沒有計劃為它添加支持,這讓我失望在一個真正的quandry,因為我找不到EF6的任何其他內存數據庫提供程序!

可能還有其他較新的引用類型字段,它也不支持,不確定。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow