1

I am facing a little problem with my NHibernate configuration connection with SQL server database. My App structure is just for simple test. Here is my hibernate.xml.cfg:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration>
    <session-factory xmlns="urn:nhibernate-configuration-2.2">
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.url">jdbc:jtds:sqlserver://OUSSEMA;DatabaseName=DB_GestionCompte</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">123</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

I am getting this error on running the project :

System.TypeInitializationException: 'The type initializer for 'HibernateUtil' threw an exception.'
HibernateConfigException: An exception occurred parsing configuration :The 'name' attribute is invalid - The value 'connection.url' is invalid according to its datatype 'Union' - The value 'connection.url' is not valid according to any of the memberTypes of the union.

Above error is raised on below code line in DataService.cs:

ISessionFactory factory = HibernateUtil.GetSessionFactory();

I tried different ways of NHibernate configurations and none of them works. Meanwhile I am sure that the SQL authentication works and cords are correct.

2 Answers 2

1

Following property names work; you need to change the values as per your configurations:

<?xml version="1.0"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.connection_string">Data Source=SQLServer;Initial Catalog=DBName;User ID=SQLUser;Password=SQLPassword</property>
        <property name="default_schema">[XYZ].[dbo]</property>
        <property name="connection.isolation">ReadCommitted</property>
        <property name="show_sql">false</property>
        <mapping assembly="MyMappingsAssembly"/>
    </session-factory>
</hibernate-configuration>

But, instead of using XML configuration, you may choose to do all this by code. Please refer to this answer which explains how to do that. The other answer explains how to log the SQL queries to file.

Sign up to request clarification or add additional context in comments.

2 Comments

i used that template of configuration and configured it for my database , meanwhile i didnt put that default schema proprety cuz i didn't know what i should i put there exacly !
ReHello, Its Fine now thanks for your help the connection is established correctly i just have a little problem with my mappings and i'll be fine thanks.
1

Looking at the documentation, it looks like your configuration is using property names that aren't recognised.

For example, connection.url, should be connection.connection_string. I cannot see equivalents in the documentation for connection.username or connection.password. These are typically contained in the connection string.

2 Comments

i just tried that after many tries atfirst it was connection_string proprety but still nothing , i was just tryin any case , and the documentation is 404 NOT FOUND
Yes. My bad. Updated with the correct link now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.