我正在从API接收一个JSON对象,该对象映射到具有引用导航属性的POCO类:
class Customer
{
public int Id { get; set; }
public Organization Organization { get; set; }
public string Name { get; set; }
// .... other properties....
}
class Organization
{
public Id { get; set; }
public string OrganizationName { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
// ... other properties...
}
我的API控制器是这样的:
public Task<Customer> Post(Customer customer)
API接收如下json:
{ Id: 1, OrganizationId: 5, Name: "Customer name" ...etc... }
Customer
对象没有属性OrganizationId
,那么我如何确保Customer
对相应Organization
的引用导航属性是正确的?
使用ASP。. NET Core 6.
Customer对象没有属性OrganizationId
这正是我们所缺少的;如果您希望能够仅通过Id
设置外键关系,则需要在类中设置外键列。
public int OrganizationId { get; set; }
到您的Customer
类,并用[ForeignKey]
数据注释修饰它(或在FluentAPI中DbContext
的OnModelCreating
方法中定义其目的),然后在导入数据时设置该值。