NHibernate mapping with forking inheritance model

此生再无相见时 提交于 2019-12-13 06:45:13

问题


I'm just starting out with NHibernate and am trying to do a mapping for my model. (this is a model-first, TDD approach, just creating the database now)

My Class hierachy is something like :

I'm trying to work out how to map this now using a mapping xml file?

This is my table and what I want them to map to

QuestionId - Int - [Question.Id]

PmqccFormId - Int - [Question.PmqccForm.Id]

InputtedAnswer - Int - [NormalQuestion.InputtedAnswer or JobVelocityQuestion.InputtedAnswer - they are both enums that will be converted to/from ints, can separate these into separate columns if need be]

ResponsibleStaffId -Int - [PiAlertQuestion.ResponsibleStaffMember.Id]

Explanation - varchar - [Question.Explanation]

QuestionType - varchar - [Discriminator]

I'm not sure how to map this given it forks a a bit. There are a heap more 'Question' objects that all inherit from YesNoQuestion or NormalQuestion but don't add anymore properties, just methods implementations.

I'm guessing I have a for the Question base class and then have ones under that? the tricky bit is the JobVelocityQuestion inheriting from Question. Can anyone point me in the right direction?


回答1:


This should get you started:

<class name="Question">
  ...
  <discriminator column="QuestionType"/>
  <subclass name="JobVelocityQuestion">
    <property name="InputtedAnswer"/>
  </subclass>
  <subclass name="NormalQuestion">
    <property name="InputtedAnswer"/>
    <subclass name="AsConsQuestion"/>
    ...
  </subclass>

I made many assumptions, but it should work. Feel free to ask further.



来源:https://stackoverflow.com/questions/3706041/nhibernate-mapping-with-forking-inheritance-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!