Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding an nHibernate mapping at run time?

Tags:

nhibernate

Background:

I'm getting a mapping failure when trying to use nHibernate. The application consists of several assemblies. One of the assemblies is a library of useful routines and the other is application code that uses the library. The library assembly adds itself to the nHibernate configuration but since it doesn't know about other assemblies it doesn't add them. My xml mapping file is located in the application assembly. I think it's not finding it because it's not looking in the application assembly.

Question: Can you map to a class in an arbitrary assembly without adding it to the configuration?

If not, can you add a mapping at run time?

Thanks

p.s. I did make sure the mapping file was marked as an embedded resource


Update - Apr 3 '09

I changed the underlying library to allow adding assemblies at initialization. That seems to work just great.

like image 706
Jay Avatar asked Dec 23 '22 12:12

Jay


2 Answers

You can add mappings at runtime at the moment your session factory is being constructed:

ISessionFactory sf = new Configuration()
    .AddFile("Item.hbm.xml")
    .AddFile("Bid.hbm.xml")
    .BuildSessionFactory();

or with assemblies:

ISessionFactory sf = new Configuration()
    .AddAssembly("NHibernate.Auction")
    .BuildSessionFactory();
like image 56
Darin Dimitrov Avatar answered Jan 07 '23 09:01

Darin Dimitrov


Re your comment - no you cannot add mappings once you constructed your session factory. You can however re-create the session factory. Keep in mind though that it can be expensive operation (a second or so).

like image 33
Rashack Avatar answered Jan 07 '23 07:01

Rashack