AutoMapper不适用于实体EF Core

时间:2018-02-07 11:35:37

标签: c# odata automapper entity-framework-core

我正在尝试使用我在expandProperties中使用.ProjectTo()指定的字段从实体获取视图。使用.Net Core 2 + Ef Core + AutoMapper

有实体:

public class LessonCatalog {
    public string Name { get; set; }
    public int? ImageId { get; set; }
    public virtual Image Image { get; set; }
    public virtual ICollection<Lesson> Lessons { get; set; }
}

public class Lesson {
    public string Name { get; set; }
    public string Description { get; set; }
    public int? ImageId { get; set; }
    public virtual Image Image { get; set; }
    public int LessonCatalogId { get; set; }
    public virtual LessonCatalog LessonCatalog { get; set; }
}

查看:

public class LessonView {
    public string Name { get; set; }
    public string Description { get; set; }
    public int? ImageId { get; set; }
    public ImageView Image { get; set; }
    public int LessonCatalogId { get; set; }
    public LessonCatalogView LessonCatalog { get; set; }
}

public class LessonCatalogView {
    public string Name { get; set; }
    public int? ImageId { get; set; }
    public ImageView Image { get; set; }
    public IEnumerable<LessonView> Lessons { get; set; }
}

我的地图:

CreateMap<LessonCatalog, LessonCatalogView>()
            .ForMember(dest => dest.Image, map => map.ExplicitExpansion())
            .ForMember(dest => dest.Lessons, map => map.ExplicitExpansion());

CreateMap<Lesson, LessonView>()
             .ForMember(dest => dest.LessonCatalog, map => map.ExplicitExpansion());

在我的存储库中:

protected readonly DbContext _context;
protected readonly DbSet<TEntity> _entities;

    public Repository(DbContext context) {
        _context = context;
        _entities = context.Set<TEntity>();
    }

public IEnumerable<TView> GetOData<TView>(ODataQueryOptions<TView> query,
        Expression<Func<TEntity, bool>> predicate = null) {

        IQueryable<TEntity> repQuery = _entities.AsQueryable();
        IQueryable res;
        if (predicate != null) repQuery = _entities.Where(predicate);

        if (query != null) {
            string[] expandProperties = GetExpands(query);
            //!!!
            res = repQuery.ProjectTo<TView>(Mapper.Configuration, expandProperties);
            //!!!
            var settings = new ODataQuerySettings();
            var ofilter = query.Filter;
            var orderBy = query.OrderBy;
            var skip = query.Skip;
            var top = query.Top;

            if (ofilter != null) res = ofilter.ApplyTo(res, settings);
            if (orderBy != null) res = orderBy.ApplyTo(res, settings);
            if (skip != null) res = skip.ApplyTo(res, settings);
            if (top != null) res = top.ApplyTo(res, settings);
        } else {
            res = repQuery.ProjectTo<TView>(Mapper.Configuration);
        }

        return (res as IQueryable<TView>).AsEnumerable();
    }

expandProperties包含“Lessons”字符串,但在.ProjectTo()之后,我在Lessons字段中为null,而对于我项目中的所有其他实体,它都是相同的。

在与实体框架相同的ASP .Net 4.5项目中,它可以正常工作。 在这种情况下,我觉得我遗漏了一些重要的东西。 可能有更好的方法来获取我在odata扩展查询中指定的属性的视图。

我的软件包:AutoMapper 6.2.2,Microsoft.EntityFrameworkCore 2.0,Microsoft.AspNetCore.OData 7.0.0-beta 1

1 个答案:

答案 0 :(得分:1)

您遇到了错误的ProjectTo重载。

repQuery.ProjectTo<TView>(Mapper.Configuration, expandProperties);

正在调用

public static IQueryable<TDestination> ProjectTo<TDestination>(
    this IQueryable source,
    IConfigurationProvider configuration,
    object parameters,
    params Expression<Func<TDestination, object>>[] membersToExpand
);

即。 expandProperties映射到object parameters参数并且没有效果。

请确保您使用ProjectTo参数调用params string[] membersToExpand重载,例如

repQuery.ProjectTo<TView>(Mapper.Configuration, null, expandProperties);

匹配

public static IQueryable<TDestination> ProjectTo<TDestination>(
    this IQueryable source,
    IConfigurationProvider configuration,
    IDictionary<string, object> parameters,
    params string[] membersToExpand
);