user2598794
user2598794

Reputation: 727

NHibernate rises NHibernate.HibernateException : Could not find named connection

I want to try a simple example with Active Record + MSSQL2012. Here is my App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <startup>
  </startup>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect, NHibernate</property>
      <property name="connection.connection_string_name">Server=(local);initial catalog=DemoDB;Integrated Security=SSPI</property>     
    </session-factory>
  </hibernate-configuration>
</configuration>

Locally I have a very simple database DemoDB with 3 tables, I've connected to if via SQL Management Studio.

Employee class:

using Castle.ActiveRecord;

namespace ActiveRecordDemo.Domain { [ActiveRecord] public class Employee : ActiveRecordBase { [PrimaryKey] public int Id { get; set; }

    [Property]
    public string FirstName { get; set; }

    [Property]
    public string LastName { get; set; }

    [BelongsTo(Type = typeof(Department), Column = "Id")]
    public Department Department
    {
        get;
        set;
    }
} }

When I run the code

ActiveRecordStarter.Initialize(ActiveRecordSectionHandler.Instance, typeof(Company), typeof(Department), typeof(Employee)); IList employees = Employee.FindAllByProperty("FirstName", firstName);

I get an error:

NHibernate.HibernateException : Could not find named connection string Server=(local);initial catalog=DemoDB;Integrated Security=SSPI

What's wrong with the connection?

Upvotes: 0

Views: 2612

Answers (2)

Wayne Evans
Wayne Evans

Reputation: 157

For me, the physical path of the root website didn't have a path. The root site is just there to hold a single application in my case. All our sites are created by code and IIS doesn't validate, it just accepts what you tell it.... including blank fields.

Opening Application Settings in IIS gave a "can't read the config" error.

Fix was Manage Website --> Advanced Settings Fill the physical path in (was blank) OK

Upvotes: 0

Jury Soldatenkov
Jury Soldatenkov

Reputation: 417

Property connection.connection_string_name forces NHibernate to search connection string among strings enumerated in standard <connectionStrings> section of App.Config. You have two options: Use property connection.connection_string instead of connection.connection_string_name, op place connection string to special section:

<connectionStrings>
  <add name="MyConnString" connectionString="Server=(local);initial catalog=DemoDB;Integrated Security=SSPI" />
    </connectionStrings>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
  <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  <property name="dialect">NHibernate.Dialect.MsSql2008Dialect, NHibernate</property>
  <property name="connection.connection_string_name">MyConnString</property>     
</session-factory>

Upvotes: 2

Related Questions