我试图通过在Automapper中使用.ForPath()
而不是获取IEnumerable并将其结果映射到Dto来在存储库中实现更优雅的调用。当我切换到此方法时,我的映射不再适用于使用//Context.Account -> Account
configuration.CreateMap<Context.Account, Account>()
.ForPath(dest => dest.BillingAddress, opt => opt.MapFrom(source => new Address(source.BillingStreet, source.BillingCity, source.BillingState, source.BillingPostalCode, source.BillingCountry)))
.ForPath(dest => dest.ShippingAddress, opt => opt.MapFrom(source => new Address(source.ShippingStreet, source.ShippingCity, source.ShippingState, source.ShippingPostalCode, source.ShippingCountry)))
.ForMember(dest => dest.Events, opt => opt.Ignore())
.ReverseMap()
.ForMember(dest => dest.BillingStreet, opt => opt.MapFrom(source => source.BillingAddress.Street))
.ForMember(dest => dest.BillingCity, opt => opt.MapFrom(source => source.BillingAddress.City))
.ForMember(dest => dest.BillingState, opt => opt.MapFrom(source => source.BillingAddress.State))
.ForMember(dest => dest.BillingPostalCode, opt => opt.MapFrom(source => source.BillingAddress.PostalCode))
.ForMember(dest => dest.BillingCountry, opt => opt.MapFrom(source => source.BillingAddress.Country))
.ForMember(dest => dest.ShippingStreet, opt => opt.MapFrom(source => source.ShippingAddress.Street))
.ForMember(dest => dest.ShippingCity, opt => opt.MapFrom(source => source.ShippingAddress.City))
.ForMember(dest => dest.ShippingState, opt => opt.MapFrom(source => source.ShippingAddress.State))
.ForMember(dest => dest.ShippingPostalCode, opt => opt.MapFrom(source => source.ShippingAddress.PostalCode))
.ForMember(dest => dest.ShippingCountry, opt => opt.MapFrom(source => source.ShippingAddress.Country));
映射的任何内容。
映射:
List<Account> accounts;
IQueryable<Context.Account> iqueryable = _dbContext.Set<Context.Account>();
if (spec != null)
{
var filter = Mapper.Map<Expression<Func<Context.Account, bool>>>(spec.FilterCriteria);
iqueryable = iqueryable.Where(filter);
if (spec is Paging<Account> paging)
{
var orderByExpression = Mapper.Map<Expression<Func<Context.Account, object>>>(paging.OrderBy);
iqueryable = iqueryable.OrderBy(orderByExpression).Take(paging.Take).Skip(paging.Skip);
}
}
accounts = iqueryable
.ToList()
.Select(x => Mapper.Map<Account>(x))
.ToList();
return accounts;
这是我的存储库中的代码:
之前(正在填充地址):
IQueryable<Account> iqueryable = _dbContext.Set<Context.Account>()
.ProjectTo<Account>();
if (spec != null)
{
if (spec is Paging<Account> paging)
{
iqueryable = paging.Prepare(iqueryable);
}
else
{
iqueryable = iqueryable.Where(spec.FilterCriteria);
}
}
return iqueryable.ToList();
之后(地址未填充):
href