Database First模式下添加Employee时出现400验证及类型转换错误
问题描述
采用Database First(数据库优先)模式开发员工添加功能,定义了Tblemployee、TblEmployeeSalary实体类,以及对应的Repository和Controller代码。调用添加员工接口时返回400状态码的验证错误,提示tblemployee字段必填,且$.tblEmployeeSalaries[0].employee无法转换为EmployeeAPI.Models.Tblemployee。
错误信息
{ "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "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."] } }
请求体
{ "id": 0, "email": "string", "password": "string", "ename": "string", "department": "string", "tblEmployeeSalaries": [ { "id": 0, "employeeId": 0, "salary": 0, "employee": "string" } ] }
Tblemployee实体类
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; } }
TblEmployeeSalary实体类
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代码
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"); } }
问题原因及解决办法
$.tblEmployeeSalaries[0].employee转换错误- 实体类
TblEmployeeSalary中的Employee是Tblemployee类型,但请求体中传入的是字符串"string",类型不匹配。 - 解决:请求体无需传递
employee字段,添加员工时薪资记录的EmployeeId会自动关联新创建的员工ID;若需手动指定,仅传递employeeId即可。修改后的请求体如下:{ "id": 0, "email": "test@example.com", "password": "123456", "ename": "张三", "department": "技术部", "tblEmployeeSalaries": [ { "id": 0, "employeeId": 0, "salary": 8000 } ] }
- 实体类
tblemployee字段必填错误- 控制器方法未明确指定从请求体绑定数据,导致模型绑定失败。需在参数上添加
[FromBody]特性:[HttpPost] public async Task<IActionResult> AddEmployee([FromBody] Tblemployee tblemployee) { // 原有逻辑代码 }
- 控制器方法未明确指定从请求体绑定数据,导致模型绑定失败。需在参数上添加
Repository未处理薪资记录
- 当前Repository的
AddEmployee方法未保存传入的薪资集合,需修改代码以级联添加薪资记录:public async Task<Tblemployee> AddEmployee(Tblemployee employee) { var tblemployee = new Tblemployee { Email = employee.Email, Password = employee.Password, Ename = employee.Ename, Department = employee.Department, TblEmployeeSalaries = employee.TblEmployeeSalaries.Select(s => new TblEmployeeSalary { Salary = s.Salary }).ToList() }; var addemployee = await _employeeContext.Tblemployees.AddAsync(tblemployee); await _employeeContext.SaveChangesAsync(); return addemployee.Entity; } - 确保数据库上下文已正确配置实体关系,支持级联保存操作。
- 当前Repository的
内容的提问来源于stack exchange,提问作者Rahul Aggarwal
相关产品推荐
相关产品推荐

