简体   繁体   English

设置关系时EF Core设置为空

[英]EF Core sets null when setting relations

I'm using entity framework core in my app. 我在我的应用程序中使用实体框架核心。 I have some relations in my tables. 我桌上有一些关系。 The problem is that when I set the relations It saves the data, but when I want to read the data, that related fields are null :( 问题是,当我设置关系时,它将保存数据,但是当我想读取数据时,相关字段为null :( 空值

Due to code limitations on Stackoverflow, I link the project you go and see full code on GH. 由于Stackoverflow上的代码限制,我链接了您进行的项目,并在GH上看到了完整的代码。

Here is my code 这是我的代码

AND

Here is the model 这是模型

I'll be so thankful if you help me. 如果您能帮助我,我将非常感激。

EDIT: Here is where I fill Data (line 140 to the end) The code 编辑:这是我填写数据的地方(第140行到最后) 代码

you should use .Include like this 您应该使用.Include这样

  public static List<Question> GetQuestion()
        {
            List<Question> x = new List<Question>();
            using (var db = new Model())
            {
                x=db.Questions.Include("Course").Include("Answer").ToList();
            }

            return x;
        }

you can also view this link for why i used .Include Entity Framework Loading Related Entities 您还可以查看此链接以了解我为什么使用.Include Entity Framework Loading Related Entities

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

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