2012-04-02 69 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