2012-04-02 8 views
0

私はいくつかの関連する質問があることを理解していますが、私はそれらの多くを見てきましたが、解決策は私の場合にはうまくいかないようです。Fluent NHibernateは自己参照の辞書をマッピングします

FluentでNH 2.1を使用しています(これは古いバージョンですが、いくつかの関連プロジェクトで共通の分母であり、アップグレードにはいくらかの作業が必要です)、私は基本的にFSMをマッピングしています。このシステムのユーザーには、一度に1つの質問が提示され、通常、そこから2つ以上の回答が選択されます。彼らの答えは次の質問につながります。答えは与えられた答えに応じて変わります。

これは(わずか消毒)は、このようなドメインの何かを作成しています。だから、私は自己参照テーブルを作成するために、ドメインで必要とされるのです

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

関連する問題