【问题标题】:How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC如何使用 ASP.NET MVC 中的实体框架将记录插入具有外键的表中
【发布时间】:2017-08-18 14:30:34
【问题描述】:

我是 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 在这里为空。我不明白这里的意思。

第二种方法:

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

但我得到一个空引用异常

对象引用未设置为对象的实例

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

所以我的问题是:

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

  2. 类似地:导航属性的作用是什么?为什么为空,什么时候不为空?

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

任何人都可以通过示例澄清一下,将不胜感激。

【问题讨论】:

  • Student 类中不应有 SubjectId。你可能在找many-to-many:一个学生可以有很多科目,一个科目可以有很多学生。
  • Student 中,确保将您的Subjects 收藏标记为虚拟。
  • @Tipx - 只有在需要lazy loading 时才需要虚拟。这不是问题。
  • 首先你需要决定你需要什么样的关系,其次你需要初始化外键对象以便能够通过外键插入
  • 查看学生/课程示例here

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


【解决方案1】:

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

Student 类中的 SubjectId 在此设置中绝对没有意义 - 因为您在 StudentSubject 之间存在 1:n 关系。您需要使用 ICollection&lt;Subject&gt; 来处理该学生注册的 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 关系。

【讨论】:

  • 我不能这样做吗,创建一个新的Subject,调用SaveChanges,下一个创建Student 检索主题并将这个subject 添加到新的Student
猜你喜欢
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多