我有一个.NET Core(6.0.1)API,是使用EF Core(6.0.1)的DB First方法构建的。每当我尝试将某个实体POST到API时,该实体中的其中一个导航属性会出现“必填字段”错误。我有几个结构类似但运行良好的其他实体,但这一个却让我遇到问题,我无法想出解决办法。有什么想法吗?
从DBContext:
modelBuilder.Entity<InsuranceCompanyStatus>(entity =>
{
entity.HasKey(e => e.InsCoStatusId);
entity.ToTable("InsuranceCompanyStatus");
entity.Property(e => e.InsCoStatusId).HasColumnName("InsCoStatusID");
entity.Property(e => e.InsuranceCompanyId).HasColumnName("InsuranceCompanyID");
entity.Property(e => e.State)
.HasMaxLength(2)
.IsUnicode(false)
.IsFixedLength();
entity.HasOne(d => d.InsuranceCompany)
.WithMany(p => p.InsuranceCompanyStatuses)
.HasForeignKey(d => d.InsuranceCompanyId)
.HasConstraintName("FK_InsuranceCompanyStatus_InsuranceCompanies");
});
脚手架模型:
public partial class InsuranceCompanyStatus
{
public int InsCoStatusId { get; set; }
public Guid InsuranceCompanyId { get; set; }
public string State { get; set; } = null!;
public bool Admitted { get; set; }
public bool? Approved { get; set; }
public virtual InsuranceCompany InsuranceCompany { get; set; } = null!;
}
尝试POST请求后的JSON数据:
{
"insuranceCompanyId": "caa3e956-a3be-4670-83e3-53a6ec47731e",
"state": "AL",
"admitted": true,
"approved": true
}
错误响应状态为400:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-cf1de6fe8e4fb67a04ff7d4c8b6a1c68-f426059123424d72-00",
"errors": {
"InsuranceCompany": [
"The InsuranceCompany field is required."
]
}
}