具有多列的流畅nHibernate映射

时间:2014-07-10 01:07:06

标签: nhibernate fluent-nhibernate many-to-many nhibernate-mapping fluent-nhibernate-mapping

我有以下数据库,我想在Fluent nhibernate中创建映射,以便我可以遍历对象集合,如

survey.question.feedback 获取所有问题的回复..我该怎么做?

Database design

到目前为止我有以下映射

 public class SurveyMapping : ClassMap<Survey>
{
    public SurveyMapping()
    {
        Id(x => x.Id, "SurveyId");
        Map(x => x.Name);
        Map(x => x.Type);
        Map(x => x.CreationDate);
        Map(x => x.ModificationDate);
        HasManyToMany<SurveyQuestions>(x => x.Questions).Table("Survey-Questions")
            .ParentKeyColumn("SurveyId").ChildKeyColumn("QuestionId").Cascade.All();
        HasManyToMany<User>(x => x.Users).Table("User-Surveys").ParentKeyColumn("SurveyId").ChildKeyColumn("UserId").Cascade.None();
    }
}

public class SurveyQuestionsMapping : ClassMap<SurveyQuestions>
{
    public SurveyQuestionsMapping()
    {
        Table("Questions");
        Id(x => x.Id, "QuestionId");
        Map(x => x.QuestionText);
        Map(x => x.CommentText);
        Map(x => x.Type);
        Map(x => x.Scale);
        Map(x => x.Rating);
        Map(x => x.Threshold);
        Map(x => x.CreationDate);
        Map(x => x.ModificationDate);
        HasMany<UserSurveyFeedback>(x => x.Feedback)
            .KeyColumn("QuestionId");
        **// This is the confusing part How I can load feedback associated with specific user here**
    }
}

1 个答案:

答案 0 :(得分:0)

您应该拥有从用户到调查,从用户到反馈,从问题到调查以及从问题到反馈的多对多关系。 至于查询用户的所有反馈,很简单:

var feedbackFromUser = session.Query<Feedback>().Where(f => f.User.UserID == userID).ToList();