NHibernate:如何在运行时重新配置映射?

时间:2021-10-26 11:30:58

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.

让我们先解决这个问题:我知道SessionFactory是不可变的——我试图在运行时更改配置并重新生成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?

但是,我看不到任何明显的删除映射的方法,除了重新生成整个配置之外,我还能做什么吗?

1 个解决方案

#1


1  

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
  • 将它序列化为MemoryStream
  • Add the "base" Customer mapping, if needed
  • 如果需要,添加“基本”客户映射
  • Create the temporary SessionFactory
  • 创建的临时SessionFactory
  • Retrieve whatever information you need to map Customer
  • 检索您需要映射客户的任何信息
  • Deserialize your saved Configuration
  • 反序列化保存的配置
  • Add the Customer mapping and create your final SessionFactory
  • 添加客户映射并创建最终的SessionFactory

#1


1  

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
  • 将它序列化为MemoryStream
  • Add the "base" Customer mapping, if needed
  • 如果需要,添加“基本”客户映射
  • Create the temporary SessionFactory
  • 创建的临时SessionFactory
  • Retrieve whatever information you need to map Customer
  • 检索您需要映射客户的任何信息
  • Deserialize your saved Configuration
  • 反序列化保存的配置
  • Add the Customer mapping and create your final SessionFactory
  • 添加客户映射并创建最终的SessionFactory