How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC

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


I'm new to Entity Framework code-first. This is my learning in ASP.NET MVC, using code-first for database creation.

I have two classes:

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

    public ICollection<Subject> Subjects { get; set; }

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

I'm trying to insert a Student record into the Student table, which has a foreign key SubjectId referencing the Subject table.

I'm trying it out in two possible ways:

First approach

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


Here I created a new Subject instance, which has SubjectId=202. Now when I create the Student object and assign value 202 to SubjectId, there is an Insert statement conflict. Though there is a Subject with SubjectId = 202, then why is there an insert conflict? And when I debug, I see that the navigation property Subjects is null here. I don't understand the point here.

Second approach:

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


But I get an a null reference exception

Object Reference not set to instance of an Object

Why is the stu.Subjects null here?

So my questions here are:

  1. What does the SubjectId in Student class mean? I.e. what does its value pertain to? Can we explicitly set it, if yes, will it refer to primary key of Subject table? If no, is it specified only for EF code conventions purpose?

  2. Similarly: what does navigation property role? Why is it null and when it will not be null?

My basic understanding of navigation property is that it is used to make EF determine the relationship between the two entities.

Can anyone please clarify a bit with examples which would be greatly appreciated.

8/18/2017 3:51:24 PM

Accepted Answer

You're basically creating a new Student, and a new Subject, in both your approaches. But from what I understand, what you're really trying to do, is create a new Student, and assign an existing Subject (with SubjectId = 202) to it - right??

The SubjectId in your Student class makes absolutely no sense in this setup - since you have a 1:n relationship between Student and Subject. You need to use that ICollection<Subject> to handle the 0:n subjects this student is enrolled in.

For that - use this code:

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

    // Add the new student to the context, and save it all.


That will do - a new student will be inserted into your database table, and the 1:n relationship between student and his subjects will be established.

8/18/2017 4:00:08 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow