开发者

Automapping subclass in Fluent NHibernate

开发者 https://www.devze.com 2022-12-13 20:28 出处:网络
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

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.


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

0

精彩评论

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