Sunday, September 21, 2008 #

Castle ActiveRecord with multiple databases

A few months ago, I came cross this problem that I had 2 databases in my project, so that my model classes might map to tables from different databases. It took me a while to work out how to deal with it in ActiveRecord. Today, I had a same problem again, even though I still got my last project’s source code, it still took me a couple of hours to get everything right. So, I think I’d better to write it done, just in case that I will have to do it again or anyone else may need it.  

The Scenario:

Database 1:      MyDatabase                             Table:  Product

Database 2:      MyProfileDatabase                   Table: UserProfile

Model Class:    Product, UserProfile

 The Code:

First, just define Product class as normal ActiveRecord model class:

[ActiveRecord]

public class Product : ActiveRecordBase<Product>

{

    [PrimaryKey("ProductID")]

    public string Id { get; set; }

   

    //...

}

 

Create an abstract base class for MyProfileDatabase.  All classes map to tables in MyProfileDatabase will inherit from this base class. Note: it has to be abstract. This was what I missed out and caused some fun time to find out.

 

public abstract class ProfileActiveRecord<T> : ActiveRecordBase<T>

{

}

 

Define UserProfile class which inherits from ProfileActiveRecord.

[ActiveRecord]

public class UserProfile : ProfileActiveRecord<UserProfile>

{

    [PrimaryKey("UserID")]

    public int Id { get; set; }

 

    //....

}

 

ActiveRecord Configration:

<activerecord>

  <config>

    <add key="hibernate.connection.driver_class"

         value="NHibernate.Driver.SqlClientDriver" />

    <add key="hibernate.dialect"

         value="NHibernate.Dialect.MsSql2005Dialect" />

    <add key="hibernate.connection.provider"

         value="NHibernate.Connection.DriverConnectionProvider" />

    <add key="hibernate.connection.connection_string"

         value="server=localhost;database=MyDatabase;Integrated Security=True" />

  </config>

 

  <config type="SomeNamespace.Core.Models.ProfileActiveRecord`1, SomeNamespace.Core">

    <add key="hibernate.connection.driver_class"

         value="NHibernate.Driver.SqlClientDriver" />

    <add key="hibernate.dialect"

         value="NHibernate.Dialect.MsSql2005Dialect" />

    <add key="hibernate.connection.provider"

         value="NHibernate.Connection.DriverConnectionProvider" />

    <add key="hibernate.connection.connection_string"

         value="server=localhost; database=MyProfileDatabase; Integrated Security=True" />

  </config>

</activerecord>

Initialize Active Records:

XmlConfigurationSource source = new XmlConfigurationSource("ActiveRecord.xml");

Type[] types =

    {

        typeof(Product),

        typeof(ProfileActiveRecord<>),

        typeof(UserProfile)

    };

ActiveRecordStarter.Initialize(source, types);

 

Posted On Sunday, September 21, 2008 7:07 AM | Comments (5)

Copyright © Changhong Fu

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski