有人可以建议我如何防止此错误。已添加具有相同键的项目。?
// Failed to find a matching SessionFactory so make a new one.
if (sessionFactory == null)
{
Check.Require(File.Exists(sessionFactoryConfigPath),
"The config file at '" + sessionFactoryConfigPath + "' could not be found");
Configuration cfg = new Configuration();
cfg.Configure(sessionFactoryConfigPath);
/*MINE*/
var persistenceModel = new PersistenceModel();
persistenceModel.AddMappingsFromAssembly(Assembly.Load("EMedicine.Core"));
persistenceModel.Configure(cfg);
/*END_OF_MINE*/
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
if (sessionFactoryConfigPath != null) sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
}
错误在这里: sessionFactory = cfg.BuildSessionFactory();
答案 0 :(得分:2)
尝试以下方法:
if (
sessionFactoryConfigPath != null &&
sessionFactories.ContainsKey(sessionFactoryConfigPath)
) {
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
} else (sessionFactoryConfigPath != null) {
sessionFactory = sessionFactories[sessionFactoryConfigPath];
}
答案 1 :(得分:0)
这个解决方案好吗?
try
{
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
if (sessionFactoryConfigPath != null)
sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
}
catch (Exception)
{
if (sessionFactoryConfigPath != null)
sessionFactory = (ISessionFactory) sessionFactories[sessionFactoryConfigPath];
}