0

I am trying to add the student entity in my database, which looks like this:

public class Student
{
    public int ID { get; set; }

    public string Name { get; set; }

    public Course Course { get; set; }
}

The Course class looks like this:

public class Course
{
    public int ID { get; set; }

    public string Name { get; set; }
}

Data in the Course table:

|   ID    |   Course   |
------------------------
|   1     |   Math     |
-----------------------
|   2     |   Physics  |
-----------------------

When I define student as shown below:

var student = new Student
{
   Name = "ABC",
   Course = new Course { ID = 1, Name = "Math"}
};

and try to add it so

context.Students.add(student);
await context.SaveChangesAsync()

the entity wasn't added. I added this line of code

context.Entry(student).State = EntityState.Added;

but nothing changed.

A.M
  • 731
  • 2
  • 7
  • 13

1 Answers1

1

First of all you have to set Foreign Key CourseId to your Student entity.

A) Student Entity

public class Student
{
    public int StudentID { get; set; }

    public string Name { get; set; }

    public int CourseID { get; set; }  //This is foreign key

    public virtual Course Course { get; set; }
}

B) Course Entity: ICollection property gives you all students with particular CourseID.

public class Course
{
    public int CourseID { get; set; }

    public string Name { get; set; }

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

1) If you add new student record with new course then

Course course = new Course
{
    Name = "Math"
};

Course insertedCourse = context.Courses.Add(course);

Student student = new Student
{
    CourseID = insertedCourse.CourseID,
    Name = "ABC"
};

context.Students.Add(student);

await context.SaveChangesAsync();

2) If you have to reference your existing course from database then you have to get course from database and assign courseId to student entity.

Course existingCourse = GetCourseByIdFromDatabase();

Student student = new Student
{
     CourseID = existingCourse.CourseID, //This ID comes from Db 
     Name = "ABC"
};

context.Students.Add(student);

await context.SaveChangesAsync();

Try once may it help you.

Edited:

As I understood with your last comment,

If you want to get student with its corresponding course then

Your get student method look like

public Student GetStudent(int Id)
{
    Student student = context.Students.Find(Id);
    return new Student
    {
        StudentID = student.StudentID,
        Name = student.Name,
        Course = student.Course
    };
}

Ad you can access student's course like

Student student = GetStudent(1);

string CourseName =   student.Course.Name;

you can use your async and await with above code depending your need.

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • Can you tell me, how get student entity to load his corresponding course too? I use include method but I get an error. – A.M Jul 22 '18 at 12:13
  • @A.M, If you get student then its virtual `Course` property gives you all about its assigned course just do it in your code like `Course.CourseID` and `Course.Name` – er-sho Jul 22 '18 at 12:49
  • @A.M, Please view edited section of answer if it help then vote up :) – er-sho Jul 22 '18 at 13:03
  • see this question: [link] https://stackoverflow.com/questions/51465177/cant-include-navigation-property-it-is-null-entity-framework-core P.S my reputation doesn't allow me to vote up :) – A.M Jul 22 '18 at 13:08
  • @A.M., did u try with edited section in answer? there all code in my working projects. – er-sho Jul 22 '18 at 13:13