I am receiving the following error using Glass Mapper
System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Glass.Mapper.AbstractService..ctor(String contextName)
at Glass.Mapper.Sc.SitecoreService..ctor(Database database, String contextName)
at Glass.Mapper.Sc.ItemExtensions.GlassCast[T](Item item, Boolean isLazy, Boolean inferType)
Here are the details of my configuration. I'm using a unit test project that has the same Glass Mapper version installed as the project it tests. My GlassMapperScCustom.cs
in the Unit Test Project is below:
public static class GlassMapperScCustom
{
public static void CastleConfig(IWindsorContainer container)
{
var config = new Config();
container.Install(new SitecoreInstaller(config));
}
public static IConfigurationLoader[] GlassLoaders()
{
return new IConfigurationLoader[] { };
}
public static void PostLoad()
{
}
}
Additionally, I have GlassMapper.Sc.Mvc.config
in the App_Config
folder and that is the only one.
The exception above occurs when the Code Under Test (CUT) calls the following line:
var paths = pathFolder.GetChildren().Select(child => child.GlassCast<Path>());
Based on what I've searched online, I need to be able to set the SitecoreContext in order for GlassMapper to work correctly. How and where would I do that; in the unit test project or the CUT project? Or is this some other issue entirely?
Edit:
Here are the Glass Mapper versions:
<package id="Glass.Mapper" version="3.0.13.25" targetFramework="net461" />
<package id="Glass.Mapper.Sc" version="3.2.2.45" targetFramework="net461" />
<package id="Glass.Mapper.Sc.CastleWindsor" version="3.2.1.23" targetFramework="net461" />
<package id="Glass.Mapper.Sc.Mvc-5" version="3.2.1.42" targetFramework="net461" />
var context = new SitecoreContext(Context.Database); var paths = pathFolder.GetChildren().Select(child => context.CreateType<=Path>(child));
which is the recommended way of glass mapping for this version of glass mapper. But the same error occurs on the first line which leads me to believe there is a problem with SitecoreFakeDb and not Glass Mapper itself. – uioporqwerty Nov 28 '16 at 17:52