views:

307

answers:

1

I'm using S#arp Architecture (which uses NHibernate). I have some entities mapped to tables in one database and others mapped to a different database. Disclosure: Databases already exist so i can't do model first.

How do I configure this to work?

EDIT: Would the SchemaIs method in Fluent NHibernate be the recommended approach to map an entity to a table in a different database? I believe this is possible via NHib's xmp mapping files too.

+1  A: 

The way I have done this is to initialise multiple NHibernateSessions in InitializeNHibernateSession within global.asax.cs using multiple nhibernate config files. I then used [Transaction("nhibernate.dbname")] (dbname being names assigned to WebSessionStorages) in the controllers against each appropriate action method.

Richard
Could you post a sample of your InitializeNHibernateSession method please? Thanks.
LordHits
Here you go...public override void Init(){base.Init();webSessionStorage1 = new WebSessionStorage(this, "nhibernate.db1");webSessionStorage2 = new WebSessionStorage(this, "nhibernate.db2");}...private void InitializeNHibernateSession(){NHibernateSession.Init(webSessionStorage1,new string[] { Server.MapPath("~/bin/MyProject.Data.dll") },Server.MapPath("~/NHibernateDB1.config"));NHibernateSession.Init(webSessionStorage2,new string[] { Server.MapPath("~/bin/MyProject.Data.dll") },Server.MapPath("~/NHibernateDB2.config"));}
Richard
@Richard: What if one controller action operates on both the database?
Amitabh
I have never done this, but perhaps the action could call two other methods in the controller with Transaction annotations referencing each DB?
Richard