Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架-将多对多相关数据加载到导航属性集合中,属性包含除Id之外的空值_C#_Entity Framework - Fatal编程技术网

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();