nhibernate配置抛出异常

时间:2011-11-04 22:22:10

标签: c# nhibernate exception

在配置(持久层)nhibernate期间,我得到一个例外。消息说nhibernate找不到配置文件hibernate.cfg.xml。但是我把我的文件扯了下来,并且它总是设置为复制到输出。我将映射和持久类存储在单独的程序集中。但是控制台项目和类库项目都在其outpt文件夹中包含配置文件。

配置文件

<?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();
    }
}

帮助!

2 个答案:

答案 0 :(得分:2)

确保存在以下* .config文件结构:

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

答案 1 :(得分:0)

尝试设置hibernate.cfg.xml的属性&#34;复制到输出目录&#34; to&#34;始终复制&#34;。