views:

3687

answers:

3

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

The error I get is: "Could not find the dialect in the configuration".

I've tried specifying settings in both app.config and hibernate.cfg.xml but neither seems to work. These files are in the same directory as my app source (tried other directories too). I've tried setting the build action on hibernate.cfg.xml as "embedded resource" but that didn't help either. I get the same error message even if I completely remove these config files.

I've looked at various examples on the net but can't get it sorted ... Does anyone know what the problem could be?

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

If you're using NHibernate 2.0, but following instructions referring to 1.2, the configuration xml has changed and this will be causing your issue.

Try (in app.config, omit the configSections for a 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>
Sam
+1  A: 

ok thanks everyone, I've got it sorted now. It seems I needed to call cfg.Configure() to process hibernate.cfg.xml ... once I did this there were a few other errors but they were all quite logical to fix up with error messages that made good 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();
}
tjjjohnson
I was having this same issue while using SQLite and your post helped me workaround it.
hitec
How to declare the mapping-assembly's name when you are using key-value pairs in App.config?
JMSA
A: 

cfg.Configure();

really helped...Thanks a lot. Earlier i was using

cfg.AddAssembly(Assembly.GetCallingAssembly());

without success

Sankalp