I have a .NET Core (6.0.1) API that was constructed using DB first method with EF Core (6.0.1). Whenever I try to POST to a specific entity, I am getting a "field is required" error for one of the navigation properties in the entity. I have several other entities structured in a similar way that seem to be working fine, but this one is causing me issues and I cannot figure it out. Any ideas?
From 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");
});
Scaffolded Model:
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!;
}
JSON from Attempted POST Body:
{
"insuranceCompanyId": "caa3e956-a3be-4670-83e3-53a6ec47731e",
"state": "AL",
"admitted": true,
"approved": true
}
Error response status is 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."
]
}
}