C# 实体框架-将多对多相关数据加载到导航属性集合中,属性包含除Id之外的空值
我有以下多对多关系:C# 实体框架-将多对多相关数据加载到导航属性集合中,属性包含除Id之外的空值,c#,entity-framework,C#,Entity Framework,我有以下多对多关系: public class Student { public int StudentId { get; set; } public string StudentName { get; set; } public virtual ICollection<Course> Courses { get; set; } } public class Course { public int CourseId { get; set; } public
public class Student {
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course {
public int CourseId { get; set; }
public string CourseName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
我的意见:
student.Courses.count=5
学生。课程[0]。课程ID=1
student.Courses[0]。CourseName=null
为什么Courses.CourseName属性为空?愚蠢的问题,但它在数据库中为空吗?哇,一点也不愚蠢;完全正确!请回答这个问题,这样我就可以做标记了。没有注意到这一点真令人尴尬。看起来在为students.Courses数据设定种子时,额外的行被插入到带有Id的Courses表中,但CourseName为null。非常感谢。我不确定我能说些什么,很高兴能帮上忙。你可以发布另一个关于播种过程中出现了什么问题的问题?非常感谢。因为你的评论,它让我看到了种子,我在Student.Course中插入了新对象,而不是插入对现有对象的引用。现在一切都好了!
var student = context.Students
.Include(x => x.Courses)
.FirstOrDefault();