Configuration
It's necessary to specify some information in order to use ActiveRecord. As a matter of fact you need to set up some NHibernate properties, ActiveRecord doesn't even know about them.
You should also pass the configuration information when starting the framework. The following sections illustrates the use of ActiveRecordStarter.Initialize class as well.
XmlConfigurationSource
The first option is to use a xml file:
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord>
So you can start the framework like this:
ActiveRecordStarter.Initialize( new XmlConfigurationSource(@"myfile.xml"), typeof(Blog), typeof(Post) );
AppDomain configuration
You can also use the xml configuration file associated with your app domain (like web.config for web apps):
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" /> </configSections> <activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord> </configuration>
You can then start the framework by using a this code:
IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource; //this for .net 1.1 IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource; //this for .net 2.0 ActiveRecordStarter.Initialize( source, typeof(Blog), typeof(Post) );
Or use:
IConfigurationSource source = ActiveRecordSectionHandler.Instance; // 1.0Rc2
InPlace configuration
The InPlace configuration is only usefull for some very specific scenarios, like testing or creating a dynamic configuration that does not come from xml.
In this case, you'll need to populate the object directly:
InPlaceConfigurationSource config = new InPlaceConfigurationSource();
config.Add( typeof(ActiveRecordBase), myproperties );
IsWeb and ThreadInfoType and IsDebug
If you're using ActiveRecord in a web application, you should use the attribute isWeb="true":
<activerecord isWeb="true"> <config> ... </config> </activerecord>
You can also provide a custom type that implements IThreadScopeInfo:
<activerecord threadinfotype="My.Namespace.CustomThreadScopeInfo, My.Assembly.Name"> <config> ... </config> </activerecord>
You can also output nhibernate mapping files to the AppDomain.BaseDirectory.
<activerecord isDebug="true"> <config> ... </config> </activerecord>
SessionFactoryHolderType
You can provide a custom type that implements ISessionFactoryHolder:
<activerecord sessionfactoryholdertype="My.Namespace.CustomSessionFacHolder, My.Assembly.Name"> <config> ... </config> </activerecord>
Examples
The following sections illustrates some usage of the Xml configuration.
MS SQLServer
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" /> </config> </activerecord>
Oracle
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Data Source=dm;User ID=dm;Password=dm;" /> </config> </activerecord>
MySQL
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Database=test;Data Source=someip;User Id=blah;Password=blah" /> </config> </activerecord>
Firebird
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.FirebirdDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.FirebirdDialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Server=localhost;Database=d:\db.fdb;User=SYSDBA;password=masterkey;ServerType=1;Pooling=false" /> <add key="hibernate.query.substitutions" value="true 1, false 0" /> </config> </activerecord>
PostgreSQL
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.PostgreSQLDialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Server=localhost;initial catalog=nhibernate;User ID=nhibernate;Password=nhibernate;" /> </config> </activerecord>
Access (Jet)
Connectivity to an Microsoft Access database via Jet requires a reference to NHibernate.JetDriver from NContrib
.
<activerecord> <config> <add key="hibernate.connection.driver_class" value="NHibernate.JetDriver.JetDriver, NHibernate.JetDriver" /> <add key="hibernate.dialect" value="NHibernate.JetDriver.JetDialect, NHibernate.JetDriver" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=nhibernate.mdb" /> </config> </activerecord>

When building NHibernate.JetDriver make sure you're linking to the some version of NHibernate as the one you are using for the project you want to use it for. Else you will get a message like this one: Die gefundene Manifestdefinition der Assembly stimmt nicht mit dem Assemblyverweis überein