Reputation: 33
I'm creating a .NET Core web API. How can I ignore reference of a supplier from product entity class when mapping an entity to DTO? From my code, you can see that I have a supplier selling collection of products. Each product got a reference to the supplier.
This is the response that I want to get.
{
"companyName": "",
"contactName": "",
"contactTitle": "",
"city": "",
"country": "",
"phone": "",
"fax": "",
"products":
[
{
"productName": "",
"supplierId": 0,
"unitPrice": 00.00,
"package": "",
"isDiscontinued": false
}
]
}
Currently, the response throws an error it gets tries to get supplier info.
{
"companyName": "",
"contactName": "",
"contactTitle": "",
"city": "",
"country": "",
"phone": "",
"fax": "",
"products":
[
{
"productName": "",
"supplierId": 0,
"unitPrice": 00.00,
"package": "",
"isDiscontinued": false,
// i get error here
"supplier" : ""
}
]
}
classes
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();
}
}
Upvotes: 0
Views: 370
Reputation: 249
Try to ignore the member:
public class SuppliersProfile: Profile
{
public SuppliersProfile()
{
// ignore Supplier reference from Product when accessing each Product
field from the ICollection product
CreateMap<Product, ProductDTO>()
.ForMember(x => x.Supplier, c => c.Ignore());
CreateMap<Supplier, SupplierDTO>().Include<Product, ProductDTO>();
.ReverseMap();
}
}
Sorry don't see that. Try to create a separate map for the product and include it in the supplier. So I'm not an expert on auto mapper. see the doku for more infors: http://docs.automapper.org/en/stable/Nested-mappings.html
Upvotes: 2