AutoMapper:使用.ProjectTo时,将参数传递给目标构造器

时间:2018-12-24 02:03:05

标签: c# automapper

我的目标对象有一个参数构造器,我需要将一个对象传递给它以供内部使用。 如何将其传递给ProjectTo

-映射

CreateMap<SomeEntity, SomeModel>();

-投射

var param = new SomeContructorParam();
await context.SomeEntities                    
                .ProjectTo<SomeModel>() --  PASS PARAM here? how so
                .ToListAsync();

目标对象

public class SomeModel
{
    private readonly SomeContructorParam ctorParam;
    public SomeModel(SomeContructorParam ctorParam)
    {
        this.ctorParam = ctorParam;
    }
    ...

}
public class SomeContructorParam
{
    ...
}

1 个答案:

答案 0 :(得分:1)

经过测试。

映射:

SomeContructorParam emptyParm = null
CreateMap<SomeEntity, SomeModel>()
    .ConstructUsing(src => new SomeModel(emptyParm));

投影

var param = new SomeContructorParam();
await context.SomeEntities                    
            .ProjectTo<SomeModel>(new { emptyParm = param })
            .ToListAsync();