[英]How to ignore nested of nested objects with ProjectTo in Automapper?
延迟加载时是否可以忽略嵌套的嵌套对象?
public class Parent
{
public List<Child> Children;
}
public class Child
{
public List<SomeObject> SomeObjects;
}
定义Parent时需要忽略Children里面的Someobject字段,但是只定义Child时不需要忽略。
如果不添加没有字段的 Child 类的副本,这可能吗? 不仅需要删除对象,而且在 SQL 查询期间忽略它。
//SomeObjects inside Child must be empty
var parent = _mapper.ProjectTo<ParentDTO>(_dbContext.Parent.Where(c => c.Id == id)).FirstOrDefault();
//not ignored SomeObjects inside Child
var child = _mapper.ProjectTo<ChildDTO>(_dbContext.Children.Where(c => c.Id == id)).FirstOrDefault();
谢谢你的时间
通过以下方式解决
CreateMap<Child, ChildDTO>
.ForMember(_ => _.SomeObjects, opt => opt.ExplicitExpansion())
.ForAllMembers(_ => _.UseDestinationValue());
//calls
var parent = _mapper.ProjectTo<ParentDTO>(_dbContext.Parents.Where(c => c.Id == id)).FirstOrDefault();
var client = _mapper.ProjectTo<ChildDTO>(
_dbContext.Children.Where(c => c.Id == id),
null,
c => c.SomeObjects //include SomeObjects
).FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.