【问题标题】:Fluent NHibernate mapping a Dictionary of self-referencesFluent NHibernate 映射自引用字典
【发布时间】:2012-04-16 07:06:10
【问题描述】:

我了解有几个相关的问题,并且我已经查看了其中的许多问题,但解决方案似乎不适用于我的情况。

我正在使用带有 Fluent 的 NH 2.1(是的,它是一个旧版本,但它是多个相关项目的共同点,并且需要一些工作才能升级),我基本上是在映射 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; }
}

所以,域要求我创建一个自引用表;上一个问题的简单外键,以及一个由 Question and Answer 键入的多对多“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)

【问题讨论】:

    标签: dictionary fluent-nhibernate mapping


    【解决方案1】:

    能否单独定义 Answer 并为其提供映射?

    有点像

    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)}
    }
    

    我只是认为这简化了映射。

    【讨论】:

    • 这是可行的,如果不理想的话。如果其他人没有想出按原样映射的方法,您将得到检查;现在,+1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    相关资源
    最近更新 更多