Hello,
I have defined 2 classes:
C#
public class Student
{
public int Id { get; set; }
public string Student_FName { get; set; }
}
public class Program
{
public int Id { get; set; }
public string ProgramName { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
}This created the tables accurately, with StudentId as FK in Program table. Now I am trying to insert data using below Json and getting error. How to solve it? I just wish to enter Maths as program for Student with Id = 1
In Controller, I am using
C#
_context.Programs.Add(request.program);
_context.SaveChanges();C#
{
"programName": "Math",
"studentId":1
}C#
{
"errors": {
"Student": [
"The Student field is required."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-7d3f8f9048ccf69b10f647fc7781f4a7-fa99172258bd6e1a-00"
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 20, 2023, 10:41 PM EST
Hello Bell,
you need to create a new
Programobject and set itsProgramName,StudentId, andStudentproperties using the corresponding values from the JSONThis is because the
Studentproperty in theProgramclass is defined as a navigation property, which means it is used to establish a relationship between theProgramandStudententities.You can try below solution.
// First, retrieve the student with the given ID from the database var student = _context.Students.Find(request.studentId); // Create a new program object and set its properties var program = new Program { ProgramName = request.programName, StudentId = request.studentId, Student = student }; // Add the program object to the context and save changes _context.Programs.Add(program); _context.SaveChanges();Thanks
Naimish Makwana
Vishal JoshiRank #377 · 6.7K reputationPosted Feb 20, 2023, 10:01 PM EST
Hello Bell,
You need to pass all fields as mentioned in the class. or you need to set nullable in entity/database. you can try the below JSON to get it to work.
{ "Id": 0, "programName": "Math", "studentId": 1, "Student": [] }Thanks
Dave Bell100 reputationPosted Feb 20, 2023, 10:37 PM EST
Sorry...that did not work. If you see Student is not a list but an object used to establist foreign key with Program.
But that gave me idea and below worked..made Student? nullable...
public class Program { public int Id { get; set; } public string ProgramName { get; set; } public int StudentId { get; set; } public Student? Student { get; set; } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 14, 2023, 9:22 PM EDT
The error message indicates that the Student field is required, which means that you need to include a Student object in the Program object that you are adding. You can modify your code to create a Student object and set its Id property to the studentId value from the JSON. Then, you can assign the Student object to the Student property of the Program object before adding it to the database. Here's an example:
var studentId = request.studentId; var student = _context.Students.Find(studentId); // If the student exists in the database if(student == null) { // Create a new student object student = new Student { Id = studentId }; } var program = new Program { ProgramName = request.programName, Student = student // Assign the student to the program }; _context.Programs.Add(program); _context.SaveChanges();This code first tries to find the Student object with the specified Id. If it exists, it uses that object. If it doesn't exist, it creates a new Student object with the specified Id. Then, it creates a new Program object and assigns the Student object to its Student property. Finally, it adds the Program object to the context and saves changes to the database.