Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error: response status is 400. The JSON value could not be converted to EmployeeAPI.Models.Tblemployee

I am trying to add Employee using Database first approach.
My tblEmployee.cs class

public partial class Tblemployee
{
    public Tblemployee()
    {
        TblEmployeeSalaries = new HashSet<TblEmployeeSalary>();
    }

    public int Id { get; set; }
    public string Email { get; set; } = null!;
    public string? Password { get; set; }
    public string? Ename { get; set; }
    public string? Department { get; set; }

    public virtual ICollection<TblEmployeeSalary> TblEmployeeSalaries { get; set; }
}

My TblEmployeeSalary class

public partial class TblEmployeeSalary
{
    public int Id { get; set; }
    public int EmployeeId { get; set; }
    public long Salary { get; set; }

    public virtual Tblemployee Employee { get; set; } = null!;
}

Employee Repository

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   public EmployeeRepository(NewEmployeeContext employeeContext)
   {
       _employeeContext = employeeContext;
   }
   public async Task<Tblemployee> AddEmployee(Tblemployee employee)
   {

       var tblemployee = new Tblemployee
       {
           Email = employee.Email,
           Password = employee.Password,
           Ename = employee.Ename,
           Department = employee.Department
          
       };
  
       var addemployee = await _employeeContext.Tblemployees.AddAsync(tblemployee);
       await _employeeContext.SaveChangesAsync();
       return addemployee.Entity;
   }

Employee Controller

 private readonly IEmployee _employee;

    public EmployeeController(IEmployee employee)
    {
        _employee = employee;
    }
    [HttpPost]
    public async Task<IActionResult> AddEmployee(Tblemployee tblemployee)
    {
        try
        {
    
            if (tblemployee == null)
            {
                return BadRequest();
            }
            var addEmployee = await _employee.AddEmployee(tblemployee);
            return Ok(addEmployee);
        }
        catch
        {
            return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving Data from Database");
        }

    }

After running the service, I am getting below error:-

{ "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1&quot;,
"title": "One or more validation errors occurred.", "status": 400,
"traceId": "00-0fd5d65a07cea14fdf89b3c367a64bf7-450f3d5145bcb3a8-00",
"errors": {
"tblemployee": [
"The tblemployee field is required."
],
"$.tblEmployeeSalaries[0].employee": [
"The JSON value could not be converted to EmployeeAPI.Models.Tblemployee. Path:
$.tblEmployeeSalaries[0].employee | LineNumber: 11 |
BytePositionInLine: 26."
] } }

My Request Body as follows:-

{
  "id": 0,
  "email": "string",
  "password": "string",
  "ename": "string",
  "department": "string",
  "tblEmployeeSalaries": [
    {
      "id": 0,
      "employeeId": 0,
      "salary": 0,
      "employee": "string"
    }
  ]
}

>Solution :

Remove the employee string as I have indicated below. It doesn’t match the class definition.

{
  "id": 0,
  "email": "string",
  "password": "string",
  "ename": "string",
  "department": "string",
  "tblEmployeeSalaries": [
    {
      "id": 0,
      "employeeId": 0,
      "salary": 0,
      "employee": "string"  // Remove this
    }
  ]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading