Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: How to-reconfigure mappings at runtime?

Let's get this out of the way first: I know that SessionFactory is immutable - I'm trying to change the Configuration at runtime and regenerate ISessionFactory.

Specifically, I have a Customer mapped that will have some fields added to its dynamic-component node at runtime. I would like to do something like this

var newSessionFactory = previousConfiguration
  .RemoveClassMapping(typeof(Customer))
  .AddXmlString(newMappingForCustomer)
  .BuildSessionFactory();

However, I don't see any obvious way to remove a mapping, is there anything I can do short of regenerating the entire Configuration?

like image 552
George Mauer Avatar asked Nov 14 '22 11:11

George Mauer


1 Answers

It's not possible. You'll have to regenerate the Configuration.

My initial suggestion would be that you choose a different strategy for your model.

However, if you are determined to go with this :-), you can:

  • Create a "partial" Configuration (that does not include Customer)
  • Serialize it to a MemoryStream
  • Add the "base" Customer mapping, if needed
  • Create the temporary SessionFactory
  • Retrieve whatever information you need to map Customer
  • Deserialize your saved Configuration
  • Add the Customer mapping and create your final SessionFactory
like image 136
Diego Mijelshon Avatar answered Dec 19 '22 20:12

Diego Mijelshon