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