Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 如何使用automapper从嵌套列表映射到目标对象?_Entity Framework_Asp.net Core_Automapper_Dto - Fatal编程技术网

Entity framework 如何使用automapper从嵌套列表映射到目标对象?

Entity framework 如何使用automapper从嵌套列表映射到目标对象?,entity-framework,asp.net-core,automapper,dto,Entity Framework,Asp.net Core,Automapper,Dto,我有这样一个源对象: public class ParentDto { public List<EntityDto> EntityDtos { get; set; } // some other stuff... } public class SomeModel { [Key] public Guid Id { get; set; } public Entity Entity { get; set; } } 在我的应用程序的另一部分中,我已

我有这样一个源对象:

public class ParentDto
{
    public List<EntityDto> EntityDtos { get; set; }
    // some other stuff...
}
public class SomeModel
{
    [Key]
    public Guid Id { get; set; }
    public Entity Entity { get; set; }
}
在我的应用程序的另一部分中,我已经使用配置文件将EntityD映射到我的实体:

CreateMap<EntityDto, Entity>()
            .ForMember(dest => dest.Member,
                opt => opt.MapFrom(src => DoSomeStuff(src.AnotherMember)))
            .ForMember(dest => dest.YetAnotherMember,
                opt => opt.MapFrom(src => DoSomeOtherStuff(src.Whatever)));
CreateMap()
.FormMember(目标=>目标成员,
opt=>opt.MapFrom(src=>DoSomeStuff(src.AnotherMember)))
.ForMember(dest=>dest.YetAnotherMember,
opt=>opt.MapFrom(src=>DoSomeOtherStuff(src.Whatever));
是否可以重用此映射以同时映射父对象Dto(包括EntityDTO列表)

CreateMap<ParentDto, SomeModel>()
            .ForMember(dest => dest.Id,
                opt => opt.Ignore())

            // some more stuff...

            // This is where I am struggling!
            .ForMember(dest => dest. Entity,
                opt => opt.MapFrom(src => src.EntityDtos[0]));
CreateMap()
.ForMember(dest=>dest.Id,
opt=>opt.Ignore())
//还有一些东西。。。
//这就是我奋斗的地方!
.FormMember(目标=>目标实体,
opt=>opt.MapFrom(src=>src.EntityDtos[0]);

我如何解决我已经有了EntityDto到Entity的映射以及我必须处理列表的事实?

一个与另一个无关。您定义的映射是根据输入到其中的对象的类型和/或指定的泛型类型param来使用的。换句话说,类似于
\u mapper.Map(parentDto)
的东西将使用
CreateMap
定义,而
\u mapper.Map(entityDto)
将使用
CreateMap
定义

现在,当AutoMapper开始映射集合属性时,默认情况下,它将使用EntityDTO->Entity mapping的定义,但如果您通过
MapFrom
指定自定义映射,则该定义将优先