Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating objects with NHibernate across multiple databases

I have one SQL Server with multiple databases. Database1 has a table with a reference to IDs that are stored in a table on Database2. Not sure if it's possible, but could I configure NHibernate (Fluent NHibernate specifically) to saturate an object pulling data from multiple databases?

I'm not concerned about writing to these tables, I'm just trying to ORM the objects to display in an data viewing application.

I realize this isn't an ideal database situation, but it's what I was given to work with.

like image 271
Chris Lees Avatar asked Jan 20 '23 19:01

Chris Lees


1 Answers

The usual answer to db-specific query structures, like cross-DB queries, is to create a view on the "local" DB (that NH connects to) that will perform the cross-DB query and return the joined results. You can also have a repository-per-DB and develop some means to query the records from each DB and join them manually.

One thing that will also work; the table property of each mapping is just a string, and could be anything; NHibernate just takes that and plugs it in wherever it needs to reference the table name. So, you could try specifying the tables in the mappings using their fully-qualified names: ConnectedDB..LocalTable, OtherDB..RemoteTable. It might be considered a hack, but it's also rather elegant in a way; your program doesn't even have to know there are multiple databases in the persistence schema.

like image 160
KeithS Avatar answered Jan 31 '23 07:01

KeithS