【发布时间】:2019-06-29 12:12:04
【问题描述】:
我的Quotation 模型包含Customer 类型的属性。当我使用以下代码从上下文中获取引用时,我想使用 automapper 填充 Customer 属性:
var quotation = context.Quotation.Include("Customer").Single(q => q.Id == 1);
var quotationDetailsViewModel = mapper.Map<QuotationDetailsViewModel>(quotation);
目前,我的quotation.Customer 已填充,但未映射到quotationDetailsViewModel 中的相应字段。我知道我必须提供一些映射,但不明白在哪里以及如何做。
这是我的模型和视图模型类:
public class Quotation
{
public long Id {get; set;}
public long CustomerId {get; set;}
public Customer Customer {get; set;}
public string Status {get; set;}
}
public class Customer
{
public long Id {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}
public class QuotationDetailsViewModel
{
public long QuotationId {get; set;}
public long CustomerId {get; set;}
public string Status {get; set;} //This is quotation status
public string Name {get; set;} //This is customer name
}
这是我的自动映射器 MappingProfile.cs
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<QuotationDetailsViewModel, Quotation>().ReverseMap();
CreateMap<QuotationDetailsViewModel, Customer>().ReverseMap();
}
}
我正在使用 .net mvc core 2.2 和 AutoMapper.Extensions.Microsoft.DependencyInjection 版本 6.1.1
【问题讨论】:
标签: c# asp.net-mvc asp.net-core automapper