忽略将实体映射到DTO的引用类型

时间:2019-04-11 10:26:46

标签: c# asp.net .net asp.net-core automapper

我正在创建.NET Core Web API。将实体映射到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();

    }
}

1 个答案:

答案 0 :(得分:2)

尝试忽略该成员:

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();

    }
}

抱歉,没有看到。尝试为产品创建单独的地图,并将其包括在供应商中。所以我不是自动映射器的专家。有关更多信息,请参见doku:http://docs.automapper.org/en/stable/Nested-mappings.html