Я создаю веб-API .NET Core. Как я могу игнорировать ссылку поставщика из класса сущности продукта при сопоставлении сущности с DTO? Из моего кода видно, что у меня есть поставщик, продающий коллекцию продуктов. Каждый товар получил ссылку на поставщика.
Это ответ, который я хочу получить.
{
"companyName": "",
"contactName": "",
"contactTitle": "",
"city": "",
"country": "",
"phone": "",
"fax": "",
"products":
[
{
"productName": "",
"supplierId": 0,
"unitPrice": 00.00,
"package": "",
"isDiscontinued": false
}
]
}
В настоящее время в ответе выдается ошибка, когда он пытается получить информацию о поставщике.
{
"companyName": "",
"contactName": "",
"contactTitle": "",
"city": "",
"country": "",
"phone": "",
"fax": "",
"products":
[
{
"productName": "",
"supplierId": 0,
"unitPrice": 00.00,
"package": "",
"isDiscontinued": false,
// i get error here
"supplier" : ""
}
]
}
классы
public class Supplier
{
public int Id { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public ICollection<Product> Products { get; set; }
}
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public int SupplierId { get; set; }
public decimal UnitPrice { get; set; }
public string Package { get; set; }]
public bool IsDiscontinued { get; set; }
public Supplier Supplier { get; set; }
}
public class SupplierDTO
{
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public ICollection<ProductDTO> Products { get; set; }
}
public class ProductDTO
{
public string ProductName { get; set; }
public int SupplierId { get; set; }
public decimal UnitPrice { get; set; }
public string Package { get; set; }
public bool IsDiscontinued { get; set; }
public SupplierDTO Supplier { get; set; }
}
public class SuppliersProfile: Profile
{
public SuppliersProfile()
{
// ignore Supplier reference from Product when accessing each Product
field from the ICollection product
CreateMap<Supplier, SupplierDTO>()
.ReverseMap();
}
}