Search code examples
c#sql-servernhibernateinvalidoperationexception

NHibernate: Instance failure during BuildSessionFactory


I'm trying NHibernate and at the beginning I get:

System.InvalidOperationException: Instance failure.

at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, Boolean isFirstTransparentAttempt, SqlAuthenticationMethod authType)
... at System.Data.SqlClient.SqlConnection.Open()
at NHibernate.Connection.DriverConnectionProvider.GetConnection()
... at NHibernate.Cfg.Configuration.BuildSessionFactory()
at NHibernateDemo.Program.Main(String[] args)

The configuration is:

<hibernate-configuration xmlns = "urn:nhibernate-configuration-2.2">
<session-factory>
  <property name = "connection.connection_string">
    Data Source=localhost\\SQLEXPRESS;Initial Catalog=NHibernateDemoDB;Integrated Security=SSPI
  </property>

  <property name = "connection.driver_class">
    NHibernate.Driver.SqlClientDriver
  </property>

  <property name = "dialect">
    NHibernate.Dialect.MsSql2012Dialect
  </property>

  <mapping assembly = "NHibernateDemo"/>
</session-factory>
</hibernate-configuration>

The code is:

    static void Main(string[] args)
    {
        var cfg = new NHibernate.Cfg.Configuration();
        cfg.Configure();

        var sessionFactory = cfg.BuildSessionFactory();

        Console.ReadLine();
    }

How can I solve it?

Maybe there is a problem in SQL Server?


Solution

  • The problem is the double quotes in the connection string: change it to this.

    Data Source=localhost\SQLEXPRESS; …
    

    Ref: Fixing "Instance Failure" when connecting to Sql Server