Chris
Chris

Reputation: 4983

Moving an NHibernate-heavy application from single-tenancy to multi-tenancy with multiple schemas

I currently have a single-tenant application that uses fluent-nhibernate mappings to map C# objects to tables in my MySQL database.

As part of an effort to bring multi-tenancy to my application, I am considering having a single schema for each tenant. That is, I will have n schemas with identical table structures, n somewhere in the hundreds.

How can I rig up NHibernate, along with my fluent mappings, so that I can arbitrarily access the correct schema, and without having to replicate my C# objects to map to each schema?

Upvotes: 0

Views: 292

Answers (1)

xanatos
xanatos

Reputation: 111940

Not tested, but you can set in the SessionFactory (technically in its configuration) the default schema name. The property is hibernate.default_schema. So you could have multiple SessionFactory, one for each schema. Clearly you mustn't set the scema name at the Entity level.

Something like:

var config = new Configuration();
... other configuration ...
config.Properties["hibernate.default_schema"] = "Foo"; // your schema name
ISessionFactory sf = config.BuildSessionFactory();

Upvotes: 1

Related Questions