当我尝试将持久性对象映射到没有默认构造函数的域模型时,我遇到了错误。
public class SetupGroup {
public List<CameraDisplayMap> Mappings { get; set; }
}
public class CameraDisplayMap {
public Camera Camera { get; private set; }
public Display Display { get; private set; }
public CameraDisplayMap(Camera camera, Display display)
...
}
public IEnumerable<CameraDisplayMap> GetSetupGroupMappings(int setupGroupId) {
return ((IQueryable<SetupGroupPto> GetAll())
.Where(x => x.Id == setupGroupId)
.Select(x => x.Mappings) // returns CameraDisplayMapPto from SetupGroupPto
.Project()
.To<CameraDisplayMap>();
}
我已经尝试使用映射配置注册了这个...
Mapper.CreateMap<CameraDisplayMapPto, CameraDisplayMap>()
.ConstructUsing(x => new CameraDisplayMap(x.Camera, x.Display));
...同时使用.ConstructUsing()和.ConvertUsing(),但它接缝这对自动播放器投影不起作用。
堆栈:
at System.Linq.Expressions.Expression.New(Type type) <---
at [..].ProjectionExpression`1.BuildExpression[TDest]()
答案 0 :(得分:4)
http://lostechies.com/jimmybogard/2014/12/23/automapper-3-3-feature-projection-conversions/
对于任何绊脚石的人,从Automapper的3.3版开始,你 可以像这样声明投影转换:
Mapper.CreateMap<CameraDisplayMapPto, CameraDisplayMap>()
----> .ConstructProjectionUsing(x => new CameraDisplayMap(x.Camera, x.Display));