我正在学习Automapper和EF Core,并偶然发现了一个问题,同时尝试建模get-only属性,该属性是基于导航属性计算的。我有两个模特:
public class Parent
{
public int Id {get;set;}
public string Name {get;set;}
public ICollection<Child> Childs {get;set;}
public int WantedChildCount {get{return Childs?.Count(c=>c.Type!=1)??0;}
}
public class Child
{
public int Id {get;set;}
public string Name {get;set;}
public int Type {get;set;}
}
一名DTO:
public class VM
{
public string Name{get;set;}
public int WantedCount{get;set;}
}
并试图从父级自动化VM,例如:
var c = context.Parents.Include(p => p.Childs).ProjectTo<VM>().FirstOrDefault();
这样配置的Automapper:
Mapper.Initialize(cfg=>
cfg.CreateMap<Parent, VM>()
.ForMember(d=>d.WantedCount, o => o.MapFrom(s=>s.WantedChildCount));
问题是,在最终的VM字段中,没有填充WantedCount。在日志中我找到了消息
The Include operation for navigation: 'p.Childs' was ignored because the target navigation is not reachable in the final query results.
因此,将此操作分为两个单独的操作很容易:首先是父级操作,然后是Mapper.Map。此外,当像这样更改Map时,它也可以工作:
cfg.CreateMap<Parent, VM>()
.ForMember(d=>d.WantedCount, o => o.MapFrom(s=>s.Childs(c=>c.Type!=1));
但这样我就完全忽略了模型中的只获取属性。我想了解问题是什么,以及如何解决它。
或者也许我应该把计算转移到DB边?但是我想避免将这个属性存储在数据库中。
发布于 2017-07-23 11:38:53
ProjectTo
是与Queryable.Select
等价的Auto,因此产生的查询属于EF核心被忽略包括类别。
与通常的IQuerable<T>
查询一样,您应该使用可以映射到SQL的显式表达式,例如,类似于这样的内容直接包含导航属性。
o.MapFrom(s => s.Childs.Count(c => c.Type != 1))
注意,虽然这应该填充所需的WantedChildCount
,但Include
仍将被忽略。
https://stackoverflow.com/questions/45264534
复制相似问题