我有 2 个具有以下关系的模型,当我想为第一个模型创建 API 帖子时,它给出了错误:
public class PositionGroup
{
[Key]
public int IdPoGr { get; set; }
[Required, MaxLength(150)]
public string Name { get; set; }
public ICollection<Position>? positions { get; set; }
}
public class Position
{
[Key]
public int IdPos { get; set; }
[Required, MaxLength(150)]
public string PositionName { get; set; }
[ForeignKey("PositionGroup")]
public int IdPosGr { get; set; }
[Column(TypeName = "date")]
public DateTime CreateDate { get; set; }
public PositionGroup? positionGroup { get; set; }
}
执行post方法时:
{
"idPoGr": 0,
"name": "G1",
"positions": [
{
"idPos": 0,
"positionName": "string",
"idPosGr": 0,
"createDate": "2024-10-02T05:17:13.314Z",
"positionGroup": "string"
}
]
}
我有一个错误:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-3b028362a6c027c0b63aa86cfee53628-50a135cc3612d607-00",
"errors": {
"positionGroup": [
"The positionGroup field is required."
],
"$.positions[0].positionGroup": [
"The JSON value could not be converted to API_CDE.Models.PositionGroup. Path: $.positions[0].positionGroup | LineNumber: 9 | BytePositionInLine: 31."
]
}
}
我该如何修复它?请帮忙。谢谢
我不知道如何修复它,因为这是我第一次必须从 .NET 5 迁移到 .NET 6
这个错误“$.positions[0].positionGroup”:[ “JSON 值无法转换为 API_CDE.Models.PositionGroup。路径:$.positions[0].positionGroup | LineNumber:9 | BytePositionInLine:31。” ] 当它无法定义模型或表时就会出现,所以简单的问题是它无法转换为这种数据类型。当您插入数据时,只需确保正确地将值分配给模型,然后插入数据并尝试。