1

我正在尝试设置一个小应用程序来在 Visual Studio 中试验 NHibernate,但我还没有走远。

我得到的错误是:“在配置中找不到方言”。

我尝试在 app.config 和 hibernate.cfg.xml 中指定设置,但似乎都不起作用。这些文件与我的应用程序源位于同一目录中(也尝试过其他目录)。我尝试将 hibernate.cfg.xml 上的构建操作设置为“嵌入式资源”,但这也无济于事。即使我完全删除了这些配置文件,我也会收到相同的错误消息。

我查看了网上的各种示例,但无法对其进行排序......有谁知道问题可能是什么?

这是我的应用程序源,app.config 和 hibernate.cfg.xml

应用程序源

using NHibernate;
using NHibernate.Cfg;

namespace Timer
{
    public partial class Form1 : Form
    {
        Configuration cfg;
        ISessionFactory factory;
        ISession session;
        ITransaction transaction;

        public Form1()
        {
            cfg = new Configuration();
            //cfg.AddAssembly("Timer");
            //cfg.AddFile("WorkoutSet.hbm.xml");

            factory = cfg.BuildSessionFactory();
            session = factory.OpenSession();
            transaction = session.BeginTransaction();

            InitializeComponent();
        }
    }
}

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section
          name="nhibernate"
          type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        />
        <section
            name="log4net"
            type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" 
        />

    </configSections>

    <nhibernate>
        <add
          key="hibernate.connection.provider"
          value="NHibernate.Connection.DriverConnectionProvider"
        />
        <add
          key="hibernate.dialect"
          value="NHibernate.Dialect.FirebirdDialect"
        />
        <add
          key="hibernate.connection.driver_class"
          value="NHibernate.Driver.FirebirdClientDriver"
        />
        <add
          key="hibernate.connection.connection_string"
          value="User=sysdba;Password=masterkey;Database=C:\X\Test\Timer\Timer.FDB;Dialect=3;ServerType=1;"
        />
    </nhibernate>

    <log4net debug="false">

        <!-- Define some output appenders -->
        <appender name="trace"
              type="log4net.Appender.TraceAppender, log4net">
            <layout type="log4net.Layout.PatternLayout,log4net">
                <param name="ConversionPattern"
                     value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
            </layout>
        </appender>

        <appender name="console"
              type="log4net.Appender.ConsoleAppender, log4net">
            <layout type="log4net.Layout.PatternLayout,log4net">
                <param name="ConversionPattern"
                     value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
            </layout>
        </appender>

        <appender name="rollingFile"
              type="log4net.Appender.RollingFileAppender,log4net" >

            <param name="File" value="h:\log.txt" />
            <param name="AppendToFile" value="false" />
            <param name="RollingStyle" value="Date" />
            <param name="DatePattern" value="yyyy.MM.dd" />
            <param name="StaticLogFileName" value="true" />

            <layout type="log4net.Layout.PatternLayout,log4net">
                <param name="ConversionPattern"
                  value="%d [%t] %-5p %c - %m%n" />
            </layout>
        </appender>

        <!-- Setup the root category, add the appenders and set the default priority -->
        <root>
            <priority value="DEBUG" />
            <appender-ref ref="console" />
        </root>

        <logger name="NHibernate">
            <level value="DEBUG" />
        </logger>


    </log4net>


    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <qualifyAssembly partialName="FirebirdSql.Data.FirebirdClient"
                fullName="FirebirdSql.Data.FirebirdClient, Version=2.0.1.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
        </assemblyBinding>
    </runtime>
</configuration>

休眠.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section
          name="nhibernate"
          type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    />
    </configSections>

    <nhibernate>
        <add
          key="hibernate.connection.provider"
          value="NHibernate.Connection.DriverConnectionProvider"
    />
        <add
          key="hibernate.dialect"
          value="NHibernate.Dialect.FirebirdDialect"
    />
        <add
          key="hibernate.connection.driver_class"
          value="NHibernate.Driver.FirebirdClientDriver"
    />
        <add
          key="hibernate.connection.connection_string"
          value="User=sysdba;Password=masterkey;Database=C:\X\Test\Timer\Timer.FDB;Dialect=3;ServerType=1;"
    />
    </nhibernate>
</configuration>
4

3 回答 3

3

好的谢谢大家,我已经整理好了。看来我需要调用 cfg.Configure() 来处理 hibernate.cfg.xml ......一旦我这样做了,就会出现一些其他错误,但它们都是非常合乎逻辑的,可以修复有意义的错误消息。

这是有效的初始化代码。

public Form1()
{
    cfg = new Configuration();
    cfg.Configure();

    factory = cfg.BuildSessionFactory();
    session = factory.OpenSession();
    transaction = session.BeginTransaction();

    InitializeComponent();
}
于 2008-12-21T19:12:59.230 回答
1

如果您使用的是 NHibernate 2.0,但按照参考 1.2 的说明进行操作,则配置 xml 已更改,这将导致您的问题。

尝试(在 app.config 中,省略configSections独立文件):

  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.FirebirdDialect</property>
      <property name="connection.driver_class">NHibernate.Driver.FirebirdClientDriver</property>
      <property name="connection.connection_string">User=sysdba;Password=masterkey;Database=C:\X\Test\Timer\Timer.FDB;Dialect=3;ServerType=1;</property>
    </session-factory>
  </hibernate-configuration>
于 2008-12-19T03:42:57.957 回答
0

cfg.Configure();

真的有帮助......非常感谢。早些时候我正在使用

cfg.AddAssembly(Assembly.GetCallingAssembly());

没有成功

于 2009-12-07T07:40:27.630 回答