System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

1,478 views
Skip to first unread message

Mikael Henriksson

unread,
Jul 11, 2009, 2:44:22 PM7/11/09
to fluent-n...@googlegroups.com
I have no idea where this is coming from. I can successfully create the whole database with:

            _sessionFactory = Fluently.Configure()
                    .Database(SQLiteConfiguration.Standard.UsingFile(PathToSqlLiteDb))
                    .Mappings(m => m.FluentMappings
                        .Add<ClubMap>()
                        .Add<ContactMap>()
                        .Add<ContactTypeMap>()
                        .Add<NewsMap>()
                        .Add<ResultMap>()
                        .Add<ResultTypeMap>()
                        .Add<ScheduleMap>()
                        .Add<TournamentMap>()
                        .Add<TournamentTypeMap>()
                        .ExportTo("D:\\"))
                        .ExposeConfiguration(BuildSchema)
                        .BuildConfiguration()
                        .BuildSessionFactory();

            _session = _sessionFactory.OpenSession();

Then after I have built the schema i goes to BuildSessionFactory and then I get an exception:

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at NHibernate.Cfg.Configuration.Mapping.GetPersistentClass(String className)
at NHibernate.Cfg.Configuration.Mapping.GetIdentifierType(String className)
at NHibernate.Type.EntityType.GetIdentifierType(IMapping factory)
at NHibernate.Type.EntityType.GetIdentifierOrUniqueKeyType(IMapping factory)
at NHibernate.Persister.Entity.AbstractPropertyMapping.InitIdentifierPropertyPaths(String pathEntityType etypeString[] columnsIMapping factory)
at NHibernate.Persister.Entity.AbstractPropertyMapping.InitPropertyPaths(String pathIType typeString[] columnsString[] formulaTemplatesIMapping factory)
at NHibernate.Persister.Entity.AbstractEntityPersister.InitOrdinaryPropertyPaths(IMapping mapping)
at NHibernate.Persister.Entity.AbstractEntityPersister.InitPropertyPaths(IMapping mapping)
at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClassICacheConcurrencyStrategy cacheISessionFactoryImplementor factoryIMapping mapping)
at NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass modelICacheConcurrencyStrategy cacheISessionFactoryImplementor factoryIMapping cfg)
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfgIMapping mappingSettings settingsEventListeners listeners)
at NHibernate.Cfg.Configuration.BuildSessionFactory()

That is by far the most retarded debug message I have ever seen :)

Paul Batum

unread,
Jul 12, 2009, 2:07:37 AM7/12/09
to fluent-n...@googlegroups.com
Yeah unfortunately NHibernate has a habit of throwing exceptions like that when there is something wrong with your configuration or your mapping. In this particular case I think its probably the mapping. Try mapping less of your entities - you need to narrow it down. If you have the NH debug symbols you can also jump into the callstack and find out the value of the 'className' argument passed to GetPersistantClass - that should give you a clue regarding which entity is causing the problem.

Paul Batum

James Gregory

unread,
Jul 12, 2009, 2:28:13 AM7/12/09
to fluent-n...@googlegroups.com
It sounds like one of your classes is referencing another entity that you haven't mapped.

Mikael Henriksson

unread,
Jul 13, 2009, 4:58:51 AM7/13/09
to fluent-n...@googlegroups.com
You were right, there was a tiny class I had not mapped. ConventionRestrictions. I mapped it but havent been able to make it work. I think it is now complaining about that my ContactPerson class inherits from Person and I mapped it as it's own class. I thought that was possible but I'll just SubClass it from Person instead.

Thanks for all the help! Paul I did what you suggested, that's how I found out!
Reply all
Reply to author
Forward
0 new messages