2012-04-02 56 views
0

我知道有几个相关的问题,我已经看了很多,但解决方案似乎并不适用于我的情况。我使用的NH 2.1与流利(是的,它是一个旧版本,但它是一个共同的几个相关项目的分母,它将需要一些工作来升级),我基本上映射一个FSM;该系统的用户每次都有一个问题,通常有两个或更多的答案可供他们选择。他们的答案导致下一个问题,根据给出的答案,这可能会有所不同。流利NHibernate映射自我参考字典

这将创建一个域这样的事情(略消毒):

public class Question 
{ 
    public virtual int Id { get; set; } 

    /// <summary> 
    /// Gets or sets the "questionnaire" Template in which this Question is asked. 
    /// </summary> 
    /// <value>The template.</value> 
    public virtual QuestionnaireTemplate Template { get; set; } 

    /// <summary> 
    /// Gets or sets a string to be displayed to the user containing the question to answer. 
    /// </summary> 
    /// <value>The question.</value> 
    public virtual string QuestionText { get; set; } 

    /// <summary> 
    /// Gets or sets a Question representing the previous question in the questionnaire. 
    /// </summary> 
    /// <value>The previous question.</value> 
    public virtual Question PreviousQuestion { get; set; } 

    /// <summary> 
    /// Gets or sets a Dictionary of Questions, each representing the question that should follow given a specified answer to the current question. 
    /// Null Values for Keys in this Dictionary represent endpoints of the questionnaire. 
    /// </summary> 
    /// <value>The next questions.</value> 
    public virtual IDictionary<string, Question> NextQuestions { get; set; } 
} 

所以,我要求的域中创建一个自引用表;一个简单的前一个问题的外键和一个由问题和答案键入的多对多“QuestionAnswers”表,其中包含下一个问题的关键,以询问当前问题的特定答案。

这里是我的映射到目前为止,基于至少一个答案字典映射的相关问题:

public TourQuestionMap() 
    { 
     Id(x => x.Id); 
     References(x => x.Template); 
     Map(x => x.QuestionText); 

     References(x => x.PreviousQuestion); 
     HasManyToMany(x => x.NextQuestions) 
      .Table("QuestionAnswers") 
      .ParentKeyColumns.Add("QuestionId", "Answer") 
      .ChildKeyColumn("NextQuestionId") 
      .AsMap("Answer") 
      .Cascade.All(); 
    } 

...但是当我尝试导出基于这个架构,我得到一个错误关于与未映射实体KeyValuePair的关联,这将表明我试图在NH中使用错误的集合构造。除了另一个映射实体的基本HasMany()映射以外,我对集合映射的经验并不丰富。

这里的基本架构之后我:

Question 
    QuestionId (int, PK, non-nullable) 
    TemplateId (int, FK to Template, not nullable, not an issue AFAIK) 
    QuestionText (string, not nullable) 
    PreviousQuestion (int, FK to Question, nullable, also not an issue AFAIK) 

QuestionAnswer (my problem child) 
    QuestionId (int, PK, FK to Question, not nullable) 
    Answer (string PK, key of Dictionary in domain, not nullable) 
    NextQuestionId (int, FK to Question, nullable) 

回答

1

您可以定义单独答案,并为它提供一个映射?

class Answer 
{ 
    public virtual int Id { get; set; } 
    public virtual string AnswerText { get; set; } 
    public virtual Question NextQuestion { get; set; } 
} 

现在问题变成

class Question 
{ 
    public virtual int Id { get; set; } 
    public virtual QuestionnaireTemplate Template { get; set; } 
    public virtual string QuestionText { get; set; } 
    public virtual Question PreviousQuestion { get; set; } 
    private List<Answer> answers 
    //Map this 
    public virtual IList<Answer> AnswerList { get return answers; } 
    public virtual IDictionary<string, Answer> Answers {get return answers.ToDictionary(a => a.AnswerText)} 
} 

我只是觉得这简化了映射。

+0

这是可行的,如果不是理想的。如果别人没有想出一个方法来映射这个,你会得到支票;现在,+1。 – KeithS 2012-04-02 17:03:48