![](/img/trans.png)
[英]ASP.NET Core Web API - How to pass specified fields as payload to third party API
[英]ASP.NET Core Web API - How to map only specified fields using auto mapper
在 ASP.NET Core-6 Web API 實體框架中,我使用 AutoMapper 將 DTO 映射到模型。 我有兩個模型 Customer 和 CustomerDetail。 客戶將只有一個 CustomerDetail :
我有這些模型:
public class Customer
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public string MobileNumber { get; set; }
public string UserType { get; set; }
public bool? IsAdmin { get; set; }
public bool? IsActive { get; set; }
public virtual CustomerDetail CustomerDetail { get; set; }
}
public class CustomerDetail
{
public string Id { get; set; }
public Guid BusinessName { get; set; }
public string AccountNumber { get; set; }
public Guid CustomerId { get; set; }
public CustomerStatus? CustomerStatus { get; set; }
public Guid? BankId { get; set; }
public decimal? ChargeValue { get; set; }
public virtual Customer Customer { get; set; }
public virtual Bank Bank { get; set; }
}
CreateCustomerDto:
public class CreateCustomerDto
{
public string BusinessName { get; set; }
public string AccountNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public string MobileNumber { get; set; }
public decimal? ChargeValue { get; set; }
}
Customer 和 CustomerDetail 同時插入到數據庫中。 因此,我將這兩個模型映射到一個 DTO (CreateCustomerDto)。
我只想將CreateCustomerDto中的字段 map 放入兩個模型中,並排除或忽略其他模型。 到目前為止,我已經這樣做了:
CreateMap<CreateCustomerDto, Customer>().ReverseMap();
CreateMap<CreateCustomerDto,CustomerDetail>().ReverseMap();
我如何實現我的目標?
謝謝
你可以像這樣忽略你想要的字段。
CreateMap<Class1, Class2>().ReverseMap()
ForMember(x => x.FieldToIgnore, opt => opt.Ignore());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.