如何使用Visual Studio和Firebird设置NHibernate?

时间:2008-12-18 23:54:17

标签: visual-studio nhibernate firebird

我正在尝试设置一个小应用程序来在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();
        }
    }
}

App.Config中

<?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>

hibernate.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>

3 个答案:

答案 0 :(得分:3)

好的,谢谢大家,我现在已经整理好了。看来我需要调用cfg.Configure()来处理hibernate.cfg.xml ...一旦我这样做了,还有一些其他的错误,但它们都很合理,可以修复错误信息。

这是有效的初始化代码。

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

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

    InitializeComponent();
}

答案 1 :(得分: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>

答案 2 :(得分:0)

  

cfg.Configure();

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

  

cfg.AddAssembly(Assembly.GetCallingAssembly());

没有成功