2011-11-04 9 views
0

(持続層)nhibernateの設定中に例外が発生します。メッセージは、nhibernateが設定ファイルhibernate.cfg.xmlを見つけることができなかったと言っています。しかし、私はファイルをチェクして、常に出力するように設定しています。別のアセンブリにマッピングと永続クラスを格納します。しかし、コンソールプロジェクトとクラスライブラリプロジェクトの両方で、outptフォルダ内に設定ファイルがあります。nhibernate設定で例外がスローされる

設定ファイル

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

ヘルプ私のサンプルコンソールアプリケーション内でこのコードを呼び出します!

答えて

1

は、以下の* .configファイル構造が存在していることを確認してください:

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

hibernate.cfg.xmlのプロパティ "Copy to Output Directory"を "Copy always"に設定してください。

+0

私はオプションがすでに設定されている以前に言ったように。構築アクション:コンテンツ出力にコピー:常にコピーします。すべてのマッピングは埋め込みリソースとして扱われます。たぶん何らかのタイプミスがありますが、私はそれを見ることができません。 – lexeme

+0

確かに。申し訳ありませんが、私は気付かなかった。 –

+0

それはOKです。気にしないで。 – lexeme

関連する問題