How do I set up NHibernate using Visual Studio and Firebird?

I'm trying to set up a small application to experiment with NHibernate in visual studio, but I'm not getting very far.

The error I am getting is: "Could not find dialect in config".

I've tried setting options in both app.config and hibernate.cfg.xml, but neither works. These files are in the same directory as my application source (tried other directories as well). I tried to set up the build action on hibernate.cfg.xml as an "embedded resource" but that didn't help either. I get the same error message even if I delete these config files completely.

I have looked through various examples on the net but cannot figure it out ... Does anyone know what the problem is?

Here is my application source, app.config and hibernate.cfg.xml

Application source

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>

      

+1


source to share


3 answers


ok thanks everyone, now i have sorted it. It seems I needed to call cfg.Configure () to handle the hibernate.cfg.xml ... once I did that there were a few more errors, but it was perfectly logical to fix the error messages that made sense.

Here's the initialization code that worked.



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

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

    InitializeComponent();
}

      

+3


source


If you are using NHibernate 2.0 but following the instructions specific to 1.2 the xml config file changed and this will cause your problem.

Try (in app.config, omit configSections

for standalone file):



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

      

+1


source


cfg.Configure ();

really helped ... Thanks a lot. I used to use

cfg.AddAssembly (Assembly.GetCallingAssembly ());

without success

0


source







All Articles