c# - 如何使用 ASP.NET MVC 中的 Entity Framework 将记录插入到具有外键的表中

标签 c# entity-framework ef-code-first-mapping

我是 Entity Framework 代码优先的新手。这是我在 ASP.NET MVC 中的学习,使用代码优先创建数据库。

我有两个类:

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int Standard { get; set; }            
    public int SubjectId { get; set; }    

    [ForeignKey("SubjectId")]
    public ICollection<Subject> Subjects { get; set; }
}

public class Subject
{
    [Key]
    public int SubjectId{ get; set; }
    public string SubjectName { get; set; }
}

我正在尝试将 Student 记录插入到 Student 表中,该表具有引用 Subject 的外键 SubjectId 表。

我正在尝试两种可能的方式:

第一种方法

using(var cxt = new SchoolContext())
{
    Subject sub = new Subject() { SubjectId = 202, SubjectName ="Geology" };
    Student stu = new Student() { Name = "Riya", SubjectId = 202 };
    cxt.Subjects.Add(sub);
    cxt.Students.Add(stu);           

    cxt.SaveChanges();
}

我在这里创建了一个新的 Subject 实例,它有 SubjectId=202。现在,当我创建 Student 对象并将值 202 赋给 SubjectId 时,出现了 Insert 语句冲突。虽然有一个 SubjectSubjectId = 202,但为什么会出现插入冲突?当我调试时,我看到导航属性 Subjects 在这里为 null。我不明白这里的重点。

第二种方法:

using( var cxt=new SchoolContext())
{
    Student stu = new Student() { Name = "Riya" };
    Subject sub = new Subject() { SubjectId = 202, SubjectName = "Geology" };
    stu.Subjects.Add(sub);
    cxt.Students.Add(stu);               

    cxt.SaveChanges();
}

但是我得到一个空引用异常

Object Reference not set to instance of an Object

为什么这里的 stu.Subjects 为空?

所以我的问题是:

  1. Student 类中的SubjectId 是什么意思? IE。它的值(value)与什么有关?我们是否可以显式设置它,如果是,它会引用 Subject 表的主键吗?如果不是,是否仅出于 EF 代码约定目的指定?

  2. 同样:导航属性的作用是什么?为什么为空,什么时候不会为空?

我对导航属性的基本理解是,它是用来让EF判断两个实体之间的关系。

谁能举例说明一下,我们将不胜感激。

最佳答案

您基本上是在创建一个 Student , 和一个新的 Subject ,在你的两种方法中。但据我了解,你真正想做的是创建一个新的 Student ,并分配一个现有的 Subject (用 SubjectId = 202 )到它 - 对吗??

SubjectId在你的Student class 在这个设置中完全没有意义——因为你在 Student 之间有一个 1:n 的关系。和 Subject .你需要使用那个 ICollection<Subject>处理该学生注册的 0:n 个科目。

为此 - 使用此代码:

using(var ctx = new SchoolContext())
{
    // create the *NEW* Student
    Student stu = new Student() { Name = "Riya" };

    // get existing subject with Id=202
    Subject sub = ctx.Subjects.FirstOrDefault(s => s.SubjectId == 202);

    // Add this existing subject to the new student's "Subjects" collection
    stu.Subjects.Add(sub);

    // Add the new student to the context, and save it all.
    ctx.Students.Add(stu);           

    ctx.SaveChanges();
}

这样就可以了 - 一个新学生将被插入到您的数据库表中,并且学生和他的科目之间将建立 1:n 关系。

关于c# - 如何使用 ASP.NET MVC 中的 Entity Framework 将记录插入到具有外键的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45759143/

相关文章:

c# - 可以查询 NotMapped 属性吗?

c# - ASP.NET Core Entity Framework 代码到数据库脚手架无法识别 Id 属性或 [Key] 注释

c# - 如何使用 System.IO.Pipelines 包创建响应 TCP 监听器?

linq - 如何使用 Linq 和 Entity Framework 连接两个可连接对象?

.net - Visual Studio 2008 中的 ADO.Net 或 Entity Framework

c# - 一对一无本金依赖EF?

.net - asp.net core 在两列上创建约束,其中一列是另一个实体

c# - 找不到 System.Windows.Point

c# - 将位类型转换为下拉列表

c# - Linq-to-Entities,比较不同字段/列中的 int 值并返回最大值