【问题标题】:An item with the same key has already been added已添加具有相同密钥的项目
【发布时间】:2011-01-27 03:13:54
【问题描述】:

有人可以建议我如何防止此错误。已添加具有相同密钥的项目。?

//  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();

【问题讨论】:

  • 你有 cfg.BuildSessionFactory() 方法的来源吗?

标签: c# nhibernate


【解决方案1】:

尝试以下方法:

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

【讨论】:

  • 感谢您的回答,但现在我在第二个 else 语句中得到对象引用未设置为对象的实例 if (interceptor != null) { session = GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession(interceptor); } else { session = GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession(); }
  • 只需检索现有的 sessionFactory ...见编辑。
  • 我将 else 语句修改为 else if (sessionFactoryConfigPath != null) { sessionFactory = (ISessionFactory) sessionFactories[sessionFactoryConfigPath]; } 但它是一样的。 if 语句会像 if (sessionFactoryConfigPath != null && !sessionFactories.ContainsValue(sessionFactory)) {
  • 如果你使用 VS,看看什么变量是空的。然后尝试找出它为什么为空。根据我从您的代码中看到的内容,我将查看上面的会话创建并尝试找出未创建会话的原因。
  • 这行一直是问题: sessionFactory = cfg.BuildSessionFactory();问题是如果同时连接到应用程序 4 次崩溃。
【解决方案2】:

这个解决方案好吗?

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

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 2011-03-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多