2

ここに画像の説明を入力 データベース、hbm マッピング ファイル、およびクラス ライブラリにある App.config があります。テスト プロジェクトからそのライブラリを参照し、作成した HibernateHelper クラスを呼び出そうとすると、実行時に次のエラーがスローされます。

NHibernate.MappingException : マッピング ドキュメントをコンパイルできませんでした: HibernateExample.Mappings.Products.hbm.xml

これは、テスト プロジェクトから参照されているクラス ライブラリであることに注意してください。出力タイプをコンソール アプリケーションに変更すると、正常に動作します。しかし、それをクラス ライブラリに戻し、テスト プロジェクトから参照すると、上記のエラーがスローされます。

config.Configure() を追加しようとしましたが、それは NhibernateDuplicateMapping 例外をスローします。

修正済み: appconfig から削除することにより、重複マッピングの問題を修正しました。また、テスト プロジェクトにも hibernate.cfg.xml ファイルを配置して、問題のマッピング エンティティを修正しました。


 public sealed class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;
    const string Connectionstring = "servicestring";

    public static void OpenSession()
    {      
        var config = new Configuration();                        
        config.Configure();
        config.AddAssembly(Assembly.GetCallingAssembly());    
        _sessionFactory = config.BuildSessionFactory();

    }

    public static ISession GetCurrentSession()
    {
        ISession session = null; 
        if (_sessionFactory == null)
            OpenSession();

        if (_sessionFactory != null)
        {
            session = _sessionFactory.OpenSession();
        }

        return session;
    }

    public static void CloseSessionFactory()
    {
        if (_sessionFactory != null)
        {
            _sessionFactory.Close();
        }
    }


    // var dsn = ConfigurationManager.ConnectionStrings[Connectionstring].ConnectionString;
    //config.SessionFactory().Integrate.Using<MsSqlCeDialect>().Connected.ByAppConfing(dsn);
    // System.Diagnostics.Debug.WriteLine("My connection string: "+dsn);
    //Get NHibernate configuration 
    //_sessionFactory = config.BuildSessionFactory();  
    //config.AddAssembly("HibernateExample");
}

何か案は?

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory>
      <property name="connection.driver_class"> NHibernate.Driver.SqlServerCeDriver</property>
        <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
      <property name="connection.connection_string">Data Source=FirstSample.sdf;</property>
      <property name="show_sql">true</property>
      <mapping assembly="HibernateExample"/>
    </session-factory>
  </hibernate-configuration>


  <connectionStrings>
        <add name="testconnectionstring"
            connectionString="Data Source=|DataDirectory|\FirstSample.sdf;Integrated Security=True"
            providerName="Microsoft.SqlServerCe.Client.3.5" />
    </connectionStrings>

  <runtime>
    <assemblyBinding xmlns="urnchemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845DCD8080CC91" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-9.0.242.0" newVersion="3.5.0.0"/>
      </dependentAssembly>
    </assemblyBinding>

  </runtime>
</configuration>

  <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateExample" namespace="HibernateExample.Domain" >
  <class name="Product" table="Products">
    <id name="Id" type="integer">
      <generator class="identity"/>
    </id>
    <property name="Name" type="string"/>
    <property name="Category" type="string"/>
    <property name="Discontinued" />
  </class>
</hibernate-mapping>

スローされた例外:

    Test 'NunitTest.TestClass.canquerydb' failed: NHibernate.MappingException : Could not compile the mapping document: HibernateExample.Mappings.Products.hbm.xml
  ----> System.InvalidOperationException : Could not find the dialect in the configuration
    at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
    at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
    at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
    at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
    at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
    at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
    at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
    NHibernateTest\NHibernateHelper.cs(21,0): at HibernateExample.NHibernateTest.NHibernateHelper.openSession()
    NHibernateTest\NHibernateHelper.cs(28,0): at HibernateExample.NHibernateTest.NHibernateHelper.GetCurrentSession()
    TestClass.cs(21,0): at NunitTest.TestClass.canquerydb()
    --InvalidOperationException
    at NHibernate.Dialect.Dialect.GetDialect(IDictionary`2 props)
    at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
4

1 に答える 1

1

Dialectエラーから、マッピングを追加する前に を設定していないようです。これは必須です。

基本的な構成コードの簡単な部分を次に示します。

var configuration = new Configuration();
configuration.SessionFactory().Integrate.Using<MsSql2012Dialect>()
                                        .Connected.ByAppConfing("connName");//sic
//now you can add the mappings
于 2013-01-24T19:28:25.833 に答える