您好,我正在将Automapper与Db IQueryable一起用于导航属性计算返回错误数据。
自动映射器映射
mp.CreateMap< OrderHistoryEntity, OrderHistory >();
mp.CreateMap< OrderEntity, Order >()
.ForMember(dest => dest.OrderHistory,
opt => opt.MapFrom(src => src.OrderHistoryEntities));
实体代码
Public Class OrderEntity
{
public OrderHistoryEntity OrderHistoryEntities{get;set;}
}
Public Class Order
{
public OrderHistory OrderHistories{get;set;}
}
方法
public IQueryable<OrderEntity> GetByPeriod(int orderId, params Expression<Func<OrderEntity, object>>[] includes)
{
IQueryable<OrderEntity> query = DbSet;
if (includes != null)
{
query = includes.Aggregate(query, (current, include) => current.Include(include));
}
return query.Where(m => m.Id == orderId);
}
Public void GetOrders(int orderId)
{
var records=GetByPeriod(orderId,m => m.OrderEntities)
_mapper.Map<List<Order>>(records);
}
Public void GetProperOrders(int orderId)
{
var records=GetByPeriod(orderId,m => m.OrderEntities)
_mapper.Map<List<Order>>(records.toList());
}
对于GetOrders,结果将为OrderEntities(Count和Data)错误 对于GetProperOrders,结果将为OrderEntities(计数和数据)正确