数据结构为:
- Table[Questionnaire] 顶部节点
- Table[QuestionGroup] 只是标题等的分组
- 表[QuestionnaireQuestion]映射[Question]<->[QuestionGroup]的多对多关系和[Questionnaire]<->[Question]的多对多关系
- 表[问题]
出于报告目的,一个问题可以存在于许多问卷/问题组中。QuestionnaireQuestion 表还包含 Question 的一些属性,这些属性可能因调查问卷而异,例如是否需要?验证等。我想不通的是如何映射 Question.Required?。该属性是唯一的,过滤时对问卷调查问卷的问题表中的问卷_id 和问题_id
当我加载问卷时,我提供了一个问卷 ID,然后给我一个 QuestionGroups 列表,每个组都包含一个问题列表(尽管缺少验证属性)。
到目前为止,我的映射是:
public class QuestionnaireMap : ClassMap<Questionnaire>
{
public QuestionnaireMap(){
WithTable("ida_questionnaire");
SetAttribute("lazy", "false");
Id(x => x.ID, "ida_questionnaire_id")
.WithUnsavedValue(0)
.GeneratedBy.UuidHex("");
Map(x => x.Description);
HasMany<QuestionGroup>(x => x.QuestionGroups)
.IsInverse()
.WithKeyColumn("ida_questionnaire_id")
.AsBag().SetAttribute("cascade", "all");
}
}
public class QuestionGroupMap : ClassMap<QuestionGroup>
{
public QuestionGroupMap()
{
WithTable("ida_questionnaire_group");
SetAttribute("lazy", "false");
Id(x => x.ID, "ida_questionnaire_group_id")
.WithUnsavedValue(0)
.GeneratedBy
.UuidHex("");
Map(x => x.Description).AsReadOnly();
//References(x => x.Parent, "ida_questionnaire_id");
HasManyToMany<Question>(x => x.Questions)
.WithTableName("ida_questionnaire_question")
.WithParentKeyColumn("ida_questionnaire_group_id")
.WithChildKeyColumn("ida_question_id")
.Cascade.All()
.IsInverse();
}
}
public class QuestionMap : ClassMap<Question>
{
public QuestionMap()
{
WithTable("ida_question");
Id(x => x.ID, "ida_question_id").WithUnsavedValue(0).GeneratedBy.UuidHex("");
Map(x => x.Description, "description").AsReadOnly();
Map(x => x.Type, "ida_question_type_id").AsReadOnly();
Component<Core.Validator>(x => x.MyValidator, m =>
{
m.Map(x => x.Type, "ida_question_type_id");
m.Map(x => x.RangeMin, "validate_min");
m.Map(x => x.RangeMax, "validate_max");
});
HasMany<Alternative>(x => x.Alternatives)
.IsInverse()
.WithKeyColumn("ida_question_id")
.AsBag().SetAttribute("cascade", "all");
}
}