简体   繁体   English

使用Entity Framework从多对多关系中选择数据

[英]Selecting data from a many-to-many relation with Entity Framework

I have a Code First EF setup with a many-to-many relation between two classes, Quiz and Question. 我有一个Code First EF设置,在Quiz和Question两个类之间具有多对多关系。 They look like this: 他们看起来像这样:

public partial class Quiz
{
    public Quiz()
    {
        Questionnaire = new HashSet<Question>();
    }

    [Key]
    public int Id { get; set; }

    [Required]
    public string Naam { get; set; }

    public ICollection<Question> Questionnaire{ get; set; }
}

public partial class Question
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Question()
    {
        Quizzes = new HashSet<Quiz>();
        Answers = new HashSet<Answer>();
    }

    [Key]
    public int Id { get; set; }

    [Column(TypeName = "ntext")]
    [Required]
    public string Content { get; set; }

    public int CategoryId { get; set; }

    public virtual ICollection<Quiz> Quizzes { get; set; }

    public virtual ICollection<Answer> Answers { get; set; }

    public virtual Category Category { get; set; }
}

I've confirmed in my database that EF correctly sets up the linking table QuizQuestions, and the seed data seems to fill in properly as well. 我已经在数据库中确认EF正确设置了链接表QuizQuestions,并且种子数据似乎也正确填充。

Now, the problem. 现在,问题了。 I have a ViewModel class that's trying to read all the quizzes in the database and add the amount of questions linked to each. 我有一个ViewModel类,它试图读取数据库中的所有测验并添加链接到每个测验的问题数量。 Problem is, the "Questionnaire" property doesn't get filled, and I can't figure out why. 问题是“ Questionnaire”属性没有填写,我不知道为什么。 The ViewModel uses the following code: ViewModel使用以下代码:

using (var context = new KwisContext())
        {
            var quizzes = context.Quizzes.ToList().Select(q => new QuizVM(q));
            Quizzes = new ObservableCollection<QuizVM>(quizzes);
        }

And QuizVM looks like this: QuizVM看起来像这样:

public class QuizVM
{
    private Quiz _q;

    public QuizVM(Quiz q)
    {
        _q = q;
    }

    public string Naam
    {
        get { return _q.Naam; }
    }

    public int QuestionCount
    {
        get {
            return _q.Questionnaire.Count;
        }
    }
}

I've compared my code with someone trying to do something similar (it's for college) but it works with his code and not with mine, and I can't for the life of me see the difference in our approach. 我已经将我的代码与试图做类似事情的人进行了比较(这是针对大学的),但是它与他的代码一起工作,而不与我的代码一起工作,而且我一生都无法看到我们方法的差异。 Is there something that I could've screwed up in Entity Framework that could cause this to fail? 我在实体框架中可能搞砸了一些可能导致失败的事情吗?

EDIT: I forgot to mention that I've tried the same code but from the other end, namely requesting every Question in the database and counting the amount of Quizzes they belong to. 编辑:我忘了提起我尝试过相同的代码,但从另一端开始,即请求数据库中的每个Question并计算它们所属的Quizz数量。 I've written this with identical code to the above, and this actually does work. 我已经使用与上面相同的代码编写了此代码,并且它确实起作用了。

明确使用Include来获取链接数据

context.Quizzes.Include(x=>x.Questionnaire).ThenInclude(x=>x.OthersThatYouNeed)......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM