简体   繁体   English

使用Entity Framework 5将现有模型添加到多对多关系

[英]Adding existing models to many-to-many relationship with Entity Framework 5

I have a table, Modules and a table Students, that have a many-to-many relationship through a pivot table ModuleStudents. 我有一个表,模块和一个学生表,它们通过数据透视表ModuleStudents具有多对多关系。 I am trying to add students to a module but encounter an error: 我正在尝试将学生添加到模块中,但是遇到错误:

An unhandled exception of type 'System.NullReferenceException' occurred in TestingEF.exe 在TestingEF.exe中发生了类型为'System.NullReferenceException'的未处理异常

Additional information: Object reference not set to an instance of an object. 附加信息:对象引用未设置为对象的实例。

SchoolContext context = new SchoolContext();

List<Module> modules = context.Modules.ToList();
Student student = context.Students.Single(s => s.MatriculationNumber == 40122047);

foreach (Module module in modules)
{
    module.Students.Add(student);
}

context.SaveChanges();

What am I doing wrong here? 我在这里做错了什么? I am fetching all the modules in the database and returning them as a list which I am then iterating over and trying to add a student to? 我正在获取数据库中的所有模块,并将它们作为列表返回,然后进行遍历并尝试向其添加学生? I don't understand why I am receiving a System.NullReferenceException 我不明白为什么会收到System.NullReferenceException

Contents of the output from the excpetion: https://gist.github.com/anonymous/bb719a215ded5518af46 摘录内容的输出内容: https ://gist.github.com/anonymous/bb719a215ded5518af46

I am using Code First for this project (a learning exercise using a Console Solution). 我正在为此项目使用Code First(使用控制台解决方案的学习练习)。

Module class: 模块类别:

class Module
{
    public int ModuleID { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

Student class: 学生班:

class Student
{
    public int StudentID { get; set; }
    public int MatriculationNumber { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<Module> Modules { get; set; }
}

Try changing your Module class creating the Students list in the default constructor, like this: 尝试更改模块类,在默认构造函数中创建“学生”列表,如下所示:

class Module
{
    public Module()
    {
        this.Students = new List<Student>();
    }

    public int ModuleID { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    public virtual Course Course { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

A call stack for the exception would be nice but if I had to make a wild guess, I would say that Students is null in this line module.Students.Add(student); 异常的调用堆栈会很好,但是如果我不得不做出一个疯狂的猜测,我会说在此行module.Students.Add(student);Studentsnull module.Students.Add(student); . Are you using code first to build your models? 您是否首先使用代码来构建模型? If so then you can initialize Students in Module's constructor; 如果是这样,那么您可以在Module's构造函数中初始化Students like so: 像这样:

public Module
{
    Students = new List<Student>();
}

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

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