开发者

How to set database schema for namespace in nhibernate

开发者 https://www.devze.com 2023-01-05 05:40 出处:网络
I have a database with multiple schemas: Security, Trade, etc. Each shema has multiple tables. Ie. Security schema has: Users, Roles etc..

I have a database with multiple schemas: Security, Trade, etc. Each shema has multiple tables. Ie. Security schema has: Users, Roles etc..

Now, can I setup nhibernate so that the schema i bound to namespace. Ie. I have a security namespace in my project with the User and Role POCOs in it. So I wont to se开发者_JS百科t bind database schema to namespace. I know i can add Schema in maping file for each class, but if I have ie. 1000 class i must specify schema for each clas.

Please help.


you can do what you want programatically right before you create your sessionfactory like this:

var cfg = new Configuration().Configure();
foreach (var pc in cfg.ClassMappings)
{//just an example
     pc.Table.Schema = pc.MappedClass.Assembly.GetName().FullName.Substring(0, 3);
}
var sessionFactory = cfg.BuildSessionFactory();

note that normally you only build your session factory once so the performance impact (if any) happens only once.


NHibernate won't do it magically. If you want to avoid changing mapping files manually, use a code-based solution like ConfORM or FluentNHibernate instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消