AutoMapper throw' Sequence不包含任何元素' (InvalidOperationException异常)

时间:2015-06-16 15:29:17

标签: c# asp.net-mvc entity-framework asp.net-web-api automapper

我尝试使用AutoMapper直接从IQueryable投影实体列表到dto,但似乎总是失败,InvalidOperationException表示The sequence contains no elements

实体

public class GatewaySetup {

    public static Expression<Func<GatewaySetup, ICollection<Parameter>>
        ParametersAccessor = gateway => gateway.ParametersStorage;

    protected virtual ICollection<Parameter> ParametersStorage { get; set; }

    public int Id { get; protected set; }

    public IEnumerable<Parameter> Parameters {
        get { return ParametersStorage.AsEnumerable(); } // linq2entities
    }

}

DTO

public class GatewaySetupDto {
    public int Id { get; set; }
    public List<ParameterDto> Parameters { get; set; }
}

映射

Mapper.CreateMap<GatewaySetup, GatewaySetupDto>()
    .ForMember(x => x.Parameters, options =>
                                    options.MapFrom(GatewaySetup.ParametersAccessor));

阿比

return _repository.Project().To<GatewaySetupDto>();

这可能是什么原因? AutoMapper是否在IEnumerable<T>List<T>之间的映射失败?

0 个答案:

没有答案