Fluent NHibernate 中的自动映射子类

发布于 2024-08-14 15:42:46 字数 1848 浏览 5 评论 0原文

我在使用流畅的 nhibernate 来正确映射我拥有的类层次结构时遇到了一些麻烦。

假设我有以下类结构

public abstract class MedicationAction
{
   ... Id and other attributes
   public virtual MedicationStatus MedStatus { get; protected set; }
}

public class CeaseAction : MedicationAction
{
   ... other properties that I want to be auto mapped
}

public class StartAction : MedicationAction
{
   ... other properties that I would like to be auto mapped  
}

所有这些类都映射到同一个表,因此我使用每个类一个表的层次结构策略。

我的自动地图覆盖如下所示:

public class MedicationActionMap : IAutoMappingOverride<MedicationAction>
{
    public void Override(AutoMapping<MedicationAction> mapping)
    {
         mapping.DiscriminateSubClassesOnColumn("MedActionTypeCode");

         mapping.SubClass<CeaseAction>("Cease");
         mapping.SubClass<StartAction>("Start");
    }

在我的 AutoPersistenceModel 生成代码中,我有以下

 return AutoMap.AssemblyOf<MedicationAction>()
      .... etc. 
      .Setup(s => 
           {
               ... etc.
               s.SubclassStrategy => SubclassStrategy.Subclass;
           }

两个问题: 1. 我是否应该对 StartAction 和 CeaseAction 类进行自动映射覆盖,并将 .Subclass 方法放在其中,而不是像我在这里那样放在 MedicateAction 自动映射覆盖中? 2. 在此自动映射生成的 hbm 中,我得到以下内容(摘录):

<class name="MedicationAction">
    ...
    <discriminator type="String">
      <column name="discriminator" />
    </discriminator>
    ...
    <subclass name="CeaseAction" />
    <subclass name="StartAction" />
    ...
 </class>

如您所见,鉴别器列和值被完全忽略。如果我删除行 (s => s.SubclassStrategy = t => SubclassStrategy.Subclass) 我会得到正确的鉴别器列,但所有子类元素都会变成连接子类元素。如何让子类策略真正选取我的鉴别器列和值?我正在使用 Fluent NHibernate 1.0 RTM。

我知道如何使用手动映射来实现此功能,但是这些类中还有很多自动映射的其他信息,我想保留这些信息。

I'm having some trouble getting fluent nhibernate to properly map the class hierarchy that I have.

Presuming that I have the following class structure

public abstract class MedicationAction
{
   ... Id and other attributes
   public virtual MedicationStatus MedStatus { get; protected set; }
}

public class CeaseAction : MedicationAction
{
   ... other properties that I want to be auto mapped
}

public class StartAction : MedicationAction
{
   ... other properties that I would like to be auto mapped  
}

All of these classes are mapped to the same table, so I'm using a table-per-class-hierarchy strategy.

The auto map override I have look like this:

public class MedicationActionMap : IAutoMappingOverride<MedicationAction>
{
    public void Override(AutoMapping<MedicationAction> mapping)
    {
         mapping.DiscriminateSubClassesOnColumn("MedActionTypeCode");

         mapping.SubClass<CeaseAction>("Cease");
         mapping.SubClass<StartAction>("Start");
    }

In my AutoPersistenceModel generation code, I have the following

 return AutoMap.AssemblyOf<MedicationAction>()
      .... etc. 
      .Setup(s => 
           {
               ... etc.
               s.SubclassStrategy => SubclassStrategy.Subclass;
           }

2 questions:
1. Should I have auto mapping overrides for the StartAction and CeaseAction classes and put the .Subclass methods in there instead of in the MedicationAction auto mapping override, like I have here?
2. In the hbm that results from this auto-mapping, I get the following (excerpt):

<class name="MedicationAction">
    ...
    <discriminator type="String">
      <column name="discriminator" />
    </discriminator>
    ...
    <subclass name="CeaseAction" />
    <subclass name="StartAction" />
    ...
 </class>

As you can see, the discriminator column and values are being completely ignored. If I remove the line (s => s.SubclassStrategy = t => SubclassStrategy.Subclass) I get the right discriminator column, but then all of the subclass elements become joined-subclass elements. How do I get the subclass strategy to actually pick up my discriminator column and values? I'm using Fluent NHibernate 1.0 RTM.

I know how to get this working using manual mappings, but there is a lot of other information in these classes that is auto mapped, and I want to keep that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-08-21 15:42:46

您的覆盖中不应包含 SubClass 调用。它们将被自动映射自动选取。

You shouldn't have the SubClass calls in your override. They will be picked up automatically by the automapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文