开发者

An item with the same key has already been added

开发者 https://www.devze.com 2022-12-23 00:05 出处:网络
Can someone advice me how to prevent this error. An item with the same key has already been added.? //Failed to find a matching SessionFactory so make a new one.

Can someone advice me how to prevent this error. An item with the same key has already been added.?

//  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*/

        开发者_C百科    //  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);
        }

Error is here: sessionFactory = cfg.BuildSessionFactory();


Try the following:

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


Is this solution OK?

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

精彩评论

暂无评论...
验证码 换一张
取 消