NHibernate: System.Argument Exception : An item with the same key has already been added

折月煮酒 提交于 2019-12-05 04:51:28

Problem is, that SessionHelper isn't thread-safe. It will potentially build several session factories (it's a bad implementation of Singleton), which in turn probably causes the error you're seeing.

I recommend using SharpArchitecture as guidance instead.

I ran into the same issue, "An item with the same key has already been added" while constructing the nhibernate configuration.

What was happening for me was that two threads were programmatically constructing different configurations, intended to connect to different databases, at the same time.

I added a lock around the entire configuration-maker, and the problem went away.

So I guess the configuration object depends on some internal global state, i.e. assumes that the configuration itself is a singleton (as i guess it would be, if it were totally file-driven, as opposed to programmatically constructed).

I realize that this is an old question but I had a similar error just a few days ago, using NHibernate 3.0.

For readers that may stumble upon this issue: this is the result of a known thread-safety problem in older versions of NHibernate. This was fixed in version 3.2 but older versions will not have the fix and may produce this problem. This bug entry describes the issue: https://nhibernate.jira.com/browse/NH-3271

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!