【发布时间】:2023-03-29 12:50:02
【问题描述】:
我正在尝试从数据库中获取学生,还包括与他们对应的课程实体,但似乎我做错了什么。
这是学生班:
public class Student
{
public int StudentID { get; set; }
public string Name { get; set; }
public int CourseID { get; set; }
public virtual Course Course { get; set; }
}
这是课程:
public class Course
{
public int CourseID { get; set; }
public string Name { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
然后我检索学生实体的集合,如下所示:
context.Students.Include(i => i.Course).ToList();
如果我删除了 include 方法,那么我会得到数据,但是 student 对象的 course 属性是 null。
P.S 我正在使用 Postman 进行测试,使用“包含”我无法得到任何东西。
如果我对此发表评论
public virtual ICollection<Student> Students { get; set; }
一切正常。
我把完整代码放在github上:
【问题讨论】:
-
我的意思是postman中显示数据“无法得到任何响应”
标签: asp.net-web-api entity-framework-core