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.