流畅的NHibernate映射自我引用的字典

时间:2012-04-02 15:49:50

标签: dictionary fluent-nhibernate mapping

我知道有几个相关的问题,我已经看过很多相关的问题,但解决方案在我的案例中似乎不起作用。

我正在使用NH 2.1和Fluent(是的它是一个旧版本,但它是几个相关项目的共同点,它需要一些工作才能升级),而我基本上是映射FSM;向该系统的用户呈现一个问题,一次一个,通常有两个或更多他们选择的答案。他们的答案导致了下一个问题,这个问题可能会根据给出的答案而有所不同。

这会创建一个像这样的域(稍微消毒):

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 个答案:

答案 0 :(得分: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)}
}

我认为这简化了映射。