Nhibernate配置抛出异常

本文关键字:抛出异常 配置 Nhibernate | 更新日期: 2023-09-27 18:13:38

在配置(持久化层)nhibernate时,我得到一个异常。消息说nhibernate找不到配置文件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.MsSql2008Dialect
    </property>
    <property name="connection.driver_class">
      NHibernate.Driver.SqlClientDriver
    </property>
    <property name="connection.connection_string">
      Data Source=(local); Initial Catalog=KrossThoughtDB;
      Integrated Security=SSPI
    </property>
    <property name="show_sql">
      true
    </property>   
    <mapping resource="MyApp.Domain.Model.Entities.Mappings.User.hbm.xml"   assembly="MyApp.Domain" />
    <mapping resource="MyApp.Domain.Model.Entities.Mappings.Blog.hbm.xml" assembly="MyApp.Domain" />
    <mapping resource="MyApp.Domain.Model.Entities.Mappings.Post.hbm.xml" assembly="MyApp.Domain" />
    <mapping resource="MyApp.Domain.Model.Entities.Mappings.Category.hbm.xml" assembly="MyApp.Domain" />
    <mapping resource="MyApp.Domain.Model.Entities.Mappings.Feedback.hbm.xml" assembly="MyApp.Domain" />
  </session-factory>
</hibernate-configuration>

我还使用了NHibernate的官方文档中提供的NHibernate会话助手实现。

会话助手

public sealed class SessionHelper
{
    private const String CurrentSessionKey = "nhibernate.current_session";
    private static readonly ISessionFactory sessionFactory;
    static SessionHelper()
    {
        sessionFactory = new Configuration().
            Configure().
            BuildSessionFactory();
    }
    public static ISession GetCurrentSession()
    {
        var context = HttpContext.Current;
        var currentSession = context.Items[CurrentSessionKey] as ISession;
        if(null == currentSession)
        {
            currentSession = sessionFactory.OpenSession();
            context.Items[CurrentSessionKey] = currentSession;
        }
        return currentSession;
    }
    public static void CloseSession()
    {
        var context = HttpContext.Current;
        var currentSession = context.Items[CurrentSessionKey] as ISession;
        if(null == currentSession)
        {               
            return;
        }
        currentSession.Close();
        context.Items.Remove(CurrentSessionKey);
    }
    public static void CloseSessionFactory()
    {
        if(null != sessionFactory)
        {
            sessionFactory.Close();
        }
    }
}

然后在示例控制台应用程序中调用这段代码

客户机代码

private static void Main(String[] args)
{
    // the next line exception's thrown
    using(var session = SessionHelper.GetCurrentSession())
    using(var tx = session.BeginTransaction())
    {
        // some actions...
        tx.Commit();
    }
}

帮助!

Nhibernate配置抛出异常

确保以下*。配置文件结构存在:

<configuration>
    <configSections>
         <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
    </configSections>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
     ....
    </hibernate-configuration>
</configuration>

尝试将hibernate.cfg.xml的"Copy to Output Directory"属性设置为"Copy always"