AutoMapper不适用于实体EF Core [英] AutoMapper don't work with entity EF Core
问题描述
我正在尝试从实体获取视图,该实体具有在.Property属性中使用.ProjectTo()指定的字段.使用.Net Core 2 + Ef Core + AutoMapper
I'm trying to get view from entity with fields that I specify at expandProperties with .ProjectTo(). Using .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
包含教训"字符串,但在.ProjectTo()
之后,我在Lessons字段中为空,并且项目中的所有其他实体都相同.
expandProperties
consist "Lessons" string, but after .ProjectTo()
I have null at Lessons field, and it's the same for all other entities at my project.
在具有Entity FrameWork的同一ASP .Net 4.5项目中,它工作正常. 在这种情况下,我觉得我错过了一些重要的事情. 可能有一种更好的方法来获取具有我在odata扩展查询中指定的属性的View.
In the same ASP .Net 4.5 project with Entity FrameWork it's works fine. I feel I missing something important, at this case. There is may be a better way to get View with properties that I specify at odata expand query.
我的软件包:AutoMapper 6.2.2,Microsoft.EntityFrameworkCore 2.0,Microsoft.AspNetCore.OData 7.0.0-beta 1
My packages: AutoMapper 6.2.2, Microsoft.EntityFrameworkCore 2.0, Microsoft.AspNetCore.OData 7.0.0-beta 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
参数,并且无效.
i.e. the expandProperties
maps to object parameters
argument and have no effect.
例如,确保调用具有params string[] membersToExpand
参数的ProjectTo
重载
Make sure you call ProjectTo
overload having params string[] membersToExpand
argument, for instance
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
);
这篇关于AutoMapper不适用于实体EF Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!