如何在投影到查询时忽略AutoMapper中的属性,但仍将其映射为结果

时间:2016-08-23 19:00:56

标签: c# entity-framework automapper

我有一个包含具有多边形的DbGeography类型的实体。我已将DbGeography映射到字符串[] []:

internal class DbGeographyAutoMapper : Profile
{
    public DbGeographyAutoMapper()
    {
        CreateMap<DbGeography, string[][]>()
            .ConvertUsing(geo =>
            {
                var maxElement = geo.ElementCount + 1;
                var rings = new string[geo.ElementCount.Value][];

                for (var elementIndex = 1; elementIndex < maxElement; elementIndex++)
                {
                    var currentElement = geo.ElementAt(elementIndex);

                    var latLngs = new string[currentElement.PointCount.Value];
                    var max = currentElement.PointCount + 1;

                    for (var i = 1; i < max; i++)
                    {
                        var point = currentElement.PointAt(i).EndPoint;
                        latLngs[i - 1] = $"{Math.Round(point.Latitude.Value, 4)},{Math.Round(point.Longitude.Value, 4)}";
                    }

                    rings[elementIndex - 1] = latLngs;
                }

                return rings;
            });
    }
}

实体到包含字符串[] []:

的模型
internal class WireCenterAutoMapper : Profile
{
    public WireCenterAutoMapper()
    {
        CreateMap<WireCenter, WirecenterModel>()
            .ForMember(m => m.Boundary, m => m.MapFrom(wc => wc.Boundary)) // This is the DbGeography to string[][]
            .ForMember(m => m.CLLI, m => m.MapFrom(wc => wc.CLLI))
            .ForMember(m => m.Id, m => m.MapFrom(wc => wc.Id))
            .ForMember(m => m.Name, m => m.MapFrom(wc => wc.OCN))
            .ForMember(m => m.Owner, m => m.MapFrom(wc => wc.Incumbent))
            .ForMember(m => m.State, m => m.MapFrom(wc => wc.State));
    }
}

当我尝试将其投射到IQueryable时......

[EnableQuery(PageSize = 500)]
public async Task<IQueryable<WirecenterModel>> Get([FromUri] BoundingBox intersects = null)
{
    return (await _wireCentersRepository.Find(intersects)).ProjectTo<WirecenterModel>();
}

...它爆炸了,因为DbGeography使用了转换。我想在将它从实体转换为模型时映射该属性,但在将其投影到EF查询时会被忽略。我把它标记为NotMapped ......

/// <summary>
///     The Wire Center's boundary
/// </summary>
[NotMapped]
public string[][] Boundary { get; set; }

......但这没有帮助。我该怎么做?

0 个答案:

没有答案