5

This is proving to be a little hard to trace down, but when using Effort for testing Entity Framework 6, I seem to get a KeyNotFoundException ("The given key was not present in the dictionary") error when trying to access one of the DBSet repositories.

I noticed it works with one or two DbSets in the DbContext, but once I start adding multiple DbSets to the DbContext, I receive the above error.

Example code (this is a simplification of my entire code, error seems random when I comment out some DbSets from the DbContext and then put them back in. I also have partial classes on the models, but it succeeds on these sometimes also, so seems weird):

Test

            [Fact]
            public void MyTest()
            {
                var connection = Effort.DbConnectionFactory.CreateTransient();
                var context = new StubDbContext(connection);

                var count = context.Models1.Count();
                Assert.Equal(count, 0);

            }

DBContext and DbSets Models

    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; }
    }

Stack Trace:

   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
Jerry
  • 4,507
  • 9
  • 50
  • 79
Stefan Zvonar
  • 3,959
  • 3
  • 24
  • 30
  • 1
    Did you reach any resolution on this? I'm running into the same kind of problem – jth41 Feb 06 '16 at 02:44
  • I have a new question here: http://stackoverflow.com/questions/35236919/entity-framework-6-runtime-error-given-key-was-not-present-in-the-dictionary – jth41 Feb 06 '16 at 03:33
  • Hi jth41, no, I ended up changing my unit tests to check behaviour rather than state, using Moq and an adapter around the DbContext for my own custom SaveChanges methods. I only noticed this problem when using Effort which uses an in-memory database rather than an installed SQL Server instance. – Stefan Zvonar Feb 07 '16 at 12:34

2 Answers2

1

I'm having the exact problem. Turned out it was because I was using the SQL geography data type in some of my SQL 2014 database tables, and effort doesn't support this data type, and there are no plans to add support for it at the moment either, which leaves me in a real quandry as I can't find any other in-memory database provider for EF6 which does!

There may potentially be other newer reference type fields that it also doesn't support, not sure.

Breeno
  • 3,007
  • 2
  • 31
  • 30
  • 1
    Update - I've started using Moq along with the EntityFramework.Testing.Moq Nuget package with considerable success. It allows me to create IQueryable collections of mocked objects to populate my DbSets with and query it in-memory with my existing DbContext queries, no issues so far. – Breeno Nov 23 '16 at 18:43
  • Hi @Breeno, Please give an example your code of how EntityFramework.Testing.Moq solved your problem. Thanks – jflaga Aug 05 '19 at 01:54
0

I have got the exact same stack trace, but the problem to me was related with the inheritance TPT of Entity Framework. If you're using it too, take a look at my answer here.

Community
  • 1
  • 1
fabriciorissetto
  • 9,475
  • 5
  • 65
  • 73