C# Fluent NHibernate - 配置持久层时发生异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1630886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:32:18  来源:igfitidea点击:

Fluent NHibernate - exception occurred during configuration of persistence layer

c#nhibernateconfigurationfluent-nhibernate

提问by inutan

I am using Fluent NHibernate with an external 'hibernate.cfg.xml' file.

我正在使用带有外部“hibernate.cfg.xml”文件的 Fluent NHibernate。

Following is the configuration code where I am getting error:

以下是我收到错误的配置代码:

       var configuration = new Configuration();
       configuration.Configure();

       _sessionFactory = Fluently.Configure(configuration)
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Template>())
                .BuildSessionFactory();

        return _sessionFactory;

But When NHibernate is trying to configure, I am getting floowing error:

但是当 NHibernate 尝试配置时,我遇到了错误:

An exception occurred during configuration of persistence layer.

配置持久层时发生异常。

The inner exception says:

内部异常说:

The ProxyFactoryFactory was not configured. Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.

未配置 ProxyFactoryFactory。使用可用的 NHibernate.ByteCode 提供程序之一初始化 session-factory 配置部分的 'proxyfactory.factory_class' 属性。

I googled and according to some solutions I found, I have made following changes:

我用谷歌搜索,根据我找到的一些解决方案,我进行了以下更改:

  1. Add following dlls to my app bin:

    Castle.Core.dll, Castle.DynamicProxy2.dll, NHibernate.ByteCode.Castle.dll

  2. Added follwing property in hibernate.cfg.xml

    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

  1. 将以下 dll 添加到我的应用程序 bin:

    Castle.Core.dll、Castle.DynamicProxy2.dll、NHibernate.ByteCode.Castle.dll

  2. 在 hibernate.cfg.xml 中添加了以下属性

    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory、NHibernate.ByteCode.Castle</property>

But still I am getting the same exception.

但我仍然遇到同样的例外。

采纳答案by Marc Climent

The problem might be in your hibernate.cfg.xml, double check that is using 2.2 version and if well formed.

问题可能出在您的 hibernate.cfg.xml 中,请仔细检查使用的是 2.2 版本以及格式是否正确。

The mapping should start like this:

映射应该像这样开始:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

Along with the error you post, maybe more information is supplied because that error is quite generic to the configuration parser. If not, maybe you can give more details of your hibernate.cfg.xml.

除了您发布的错误之外,可能还提供了更多信息,因为该错误对于配置解析器来说非常普遍。如果没有,也许您可​​以提供有关 hibernate.cfg.xml 的更多详细信息。

回答by inutan

Well, I was able to resolve that error by placing .cfg.xml file in bin of calling app.

好吧,我能够通过将 .cfg.xml 文件放在调用应用程序的 bin 中来解决该错误。

But now, I am getting another error :-(

但是现在,我遇到了另一个错误:-(

FluentNHibernate.Cfg.FluentConfigurationException was unhandled Message: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

FluentNHibernate.Cfg.FluentConfigurationException 未处理消息:创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 以获取更多详细信息。

* Database was not configured through Database method.

*数据库不是通过数据库方法配置的。

Here is my hibernate.cfg.xml

这是我的 hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=dev\sql2005;Initial Catalog=TestDB;Integrated Security=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration> 

Any thoughts?

有什么想法吗?

回答by kkmct

I had this error too. It fires when you don't copy the mapping file (hibernate.cfg.xml) to the build directory.

我也有这个错误。当您不将映射文件 (hibernate.cfg.xml) 复制到构建目录时,它会触发。

Solution:

解决方案:

  • In Solution explorer, right clickon the mapping file (hibernate.cfg.xml), choose Properties, then make sure that Copy To Output Directoryhas Copy if newerselected).
  • 在解决方案资源管理器中,右键单击映射文件 ( hibernate.cfg.xml),选择Properties,然后确保Copy To Output Directory具有Copy if newerselected)。

回答by Alex InTechno

Exception with the text of PotentialReasons

* Database was not configured through Database method.

might be also thrown by the FluentConfiguration.BuildConfiguration() method in the case your mappings are wrong(i.e. .*Map classes were not successfully parsed) and you have configured database not with .Database() method. This is a bit confusingly that it is discovered only at the step of building configuration, not when these classes are being added from assembly (by AddFromAssemblyOf<>)

PotentialReasons 文本异常

* 未通过 Database 方法配置数据库。如果您的映射错误(即 .*Map 类未成功解析)并且您没有使用 .Database() 方法配置数据库,则 FluentConfiguration.BuildConfiguration() 方法

也可能抛出。这有点令人困惑,它仅在构建配置的步骤中被发现,而不是在从程序集中添加这些类时(通过 AddFromAssemblyOf<>)

You can check whether your *Map classes are successfully converted to HBM by executing a line m.FluentMappings.Add(typeof(YourMappedTypeMap)).ExportTo(@"c:\Temp\fluentmaps"))

您可以通过执行一行m.FluentMappings.Add(typeof(YourMappedTypeMap)).ExportTo(@"c:\Temp\fluentmaps"))来检查您的 *Map 类是否成功转换为 HBM

回答by Jakobus

Trying to configure the database in App.config and the mappings via Fluent NHibernate, I also got a FluentNHibernateException saying

尝试在 App.config 中配置数据库并通过 Fluent NHibernate 进行映射时,我也收到了 FluentNHibernateException 说

  • Database was not configured through Database method.
  • 未通过 Database 方法配置数据库。

The link to show details was deactivated, so I couldn't get any further information. Searching for hours I finally found out that my connection string had an error ("pasword=xyz;" instead of "password=xyz;")

显示详细信息的链接已停用,因此我无法获得更多信息。搜索了几个小时,我终于发现我的连接字符串有错误(“密码=xyz;”而不是“密码=xyz;”)

回答by hoonzis

As Alex InTechno said, this might be the error in the definition of your Mapping files or in mapped entities. I experienced the same error, when I have forgot that all properties in mapped classes have to be defined as virtual.

正如 Alex InTechno 所说,这可能是映射文件或映射实体定义中的错误。当我忘记映射类中的所有属性都必须定义为虚拟时,我遇到了同样的错误

public String Name {get;set;}
public virtual String Name { get; set; }

回答by user2821044

I had the same issue with NUnit tests under Resharper 8.1

我在 Resharper 8.1 下的 NUnit 测试遇到了同样的问题

Tick "ReSharper | Options | Tools | Unit Testing | Use separate AppDomain for each assembly with tests" checkbox fixed it

勾选“ReSharper | 选项 | 工具 | 单元测试 | 为每个程序集使用单独的 AppDomain 和测试”复选框修复它

回答by NarsilNarsilNarsilNarsil Narsi

Try to set modifier protected in set_id and set_sampelList. for example:

尝试在 set_id 和 set_sampleList 中设置受保护的修饰符。例如:

public virtual int Id {
    get; protected set;
}

and

public virtual IList<Store> StoresStockedIn {
    get; protected set;
}

回答by Rob

I begin receiving the

我开始收到

* Database was not configured through Database method.

* 数据库不是通过数据库方法配置的。

error message while attempting to connect to a DB/2 database without having made any changes to my code. After verifying that the configuration file was being copied to the build directory and verifying that my XML was well-formed and conformed to version 2.2, I finally went to check the database to ensure that nothing had changed.

在未对代码进行任何更改的情况下尝试连接到 DB/2 数据库时出现错误消息。在验证配置文件被复制到构建目录并验证我的 XML 格式正确且符合 2.2 版本后,我最后去检查数据库以确保没有任何更改。

It turned out that the password for the connecting account had expired. It wasn't invalid nor was the account inactive--the password was merely expired. I am not sure why the expired password manifested in such an odd error message.

原来连接帐户的密码已过期。它不是无效的,也不是帐户处于非活动状态——密码只是过期了。我不确定为什么过期的密码会出现在如此奇怪的错误消息中。

回答by Darian Everett

It seems like this error can be a bunch of different things. I got the same error and eventually figured out it was because I had excluded an unused view and mapping from the project. No idea how this was causing the error but as soon as I added it back the error was gone.

似乎这个错误可能是一堆不同的东西。我遇到了同样的错误,最终发现这是因为我从项目中排除了一个未使用的视图和映射。不知道这是如何导致错误的,但是一旦我将其添加回来,错误就消失了。