Xml Configuration Reference
The following is the definition of the expected xml schema. Differences regarding the how it must appear in a standalone xml file or in a configuration associated with an AppDomain (web.config for instance) are also explained below.
<configuration> <activerecord isWeb="true|false" isDebug="true|false" pluralizeTableNames="true|false" threadinfotype="custom thread info implementation" sessionfactoryholdertype="custom session holder implementation" namingstrategytype="custom namingstrategy implementation"> <config> <add key="connection.driver_class" value="NHibernate Driver" /> <add key="dialect" value="NHibernate Dialect" /> <add key="connection.provider" value="NHibernate Connection Provider" /> <!-- Use only one of the two attributes below --> <add key="connection.connection_string" value="connection string" /> <add key="connection.connection_string_name" value="name of connection string in config" /> </config> <config type="Full Type name to Abstract Class that defines boundaries for different database"> <add key="connection.driver_class" value="NHibernate Driver" /> <add key="dialect" value="NHibernate Dialect" /> <add key="connection.provider" value="NHibernate Connection Provider" /> <!-- Use only one of the two attributes below --> <add key="connection.connection_string" value="connection string" /> <add key="connection.connection_string_name" value="name of connection string in config" /> </config> </activerecord> </configuration>
The following table explains the attributes.
| Attribute | Required? | Description |
|---|---|---|
| isWeb | No | If ActiveRecord is running in a ASP.Net application, you must add this attribute with the value true |
| isDebug | No | If true forces ActiveRecord to write the mappings to files. Useful for debugging and to show NHibernate team when asking for help on their forum. The files are written to the bin folder. |
| pluralizeTableNames | No | If true, ActiveRecord will infer the table names as plural of the entity name. Defaults to false. |
| threadinfotype | No | Full qualified type name to a custom implementation of IThreadScopeInfo |
| sessionfactoryholdertype | No | Full qualified type name to a custom implementation of ISessionFactoryHolder |
| namingstrategytype | No | Full qualified type name to a custom implementation of INamingStrategy. This is a NHibernate interface |
| type (on config node) | No | Only required if more than one config node is present. Should be a fully qualified type name to an abstract class that extends ActiveRecordBase and defines the boundaries to a different database |
The NHibernate config elements are rather static and can be used from the examples below. The only element to pay attention to is the connection information. There are two possibilities two specify how to connect to the database:
- Adding a connection string using the key connection.connection_string
- Using a predefined named connection string by specifying connection.connection_string_name
<configuration> <configSections> <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" /> </configSections> <connectionStrings> <add name="main" connectionString="Data Source=.;Initial Catalog=test;Integrated Security=SSPI"/> </connectionStrings> <activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string_name" value="main" /> </config> </activerecord> </configuration>
Configuration on standalone xml file
All that is required in a standalone configuration file is that the activerecord node be the root element node. For example:
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord>
AppDomain configuration
Every AppDomain has a configuration file associated with it. For web application the file will be web.config. For .net executables it will be the name of the executable file and the sufix .config, for example myapp.exe.config.
You can use these files to add a configuration to Castle ActiveRecord. Just make sure you declare a section for it under the sections node.
<configuration> <configSections> <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" /> </configSections> <activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord> </configuration>
Examples per Database
The following sections illustrates some usage of the Xml configuration.
Microsoft SQL Server 2000
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord>
Microsoft SQL Server 2005
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord>
Oracle
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.OracleClientDriver" /> <add key="dialect" value="NHibernate.Dialect.OracleDialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Data Source=dm;User ID=dm;Password=dm;" /> </config> </activerecord>
MySQL
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" /> <add key="dialect" value="NHibernate.Dialect.MySQLDialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Database=test;Data Source=someip;User Id=blah;Password=blah" /> </config> </activerecord>
Firebird
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.FirebirdDriver" /> <add key="dialect" value="NHibernate.Dialect.FirebirdDialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Server=localhost;Database=d:\db.fdb;User=name;password=masterkey;ServerType=1;Pooling=false" /> <add key="query.substitutions" value="true 1, false 0" /> </config> </activerecord>
PostgreSQL
<activerecord> <config> <add key="connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" /> <add key="dialect" value="NHibernate.Dialect.PostgreSQLDialect" /> <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="connection.connection_string" value="Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=nhibernate;" /> </config> </activerecord>
See also
Check the User's guide Configuration and Initialization article.