【发布时间】:2022-11-13 23:23:39
【问题描述】:
我正在尝试创建 Post API,它具有以下模型作为请求
public partial class Employee
{
public Employee()
{
EmployeeDetails = new HashSet<EmployeeDetail>();
}
public int Id { get; set; }
public string? Name { get; set; }
public string? Gender { get; set; }
public int Age { get; set; }
public virtual ICollection<EmployeeDetail> EmployeeDetails { get; set; }
}
public partial class EmployeeDetail
{
public int Id { get; set; }
public int? EmployeeId { get; set; }
public string? Details { get; set; }
public virtual Employee Employee { get; set; }
}
我有和 Post API 接受参数 EmployeeDetail
public async Task<IActionResult> Post(EmployeeDetail empDetail){
}
so for this getting error :
Bad Request 400.
插入 empDetail 的代码,(假设 Employee 已经存在于数据库中,因此不将任何值与 Employee 对象绑定。并基于 EmployeeId,在 employeeDetail 表中插入 Employee 详细信息。)
This is what swagger suggesting for body request.
{
"id": 0,
"employeeId": 0,
"details": "string",
"employee": {
"id": 0,
"name": "string",
"gender": "string",
"age": 0,
"employeeDetails": [
"string"
]
}
}
//But I only want to pass
{
"id": 0,
"employeeId": 0,
"details": "string"
}
任何解决方案提前致谢。
【问题讨论】:
标签: .net entity-framework-core asp.net-core-mvc