C# 如何将单个属性映射到每个元素';使用AutoMapper的集合的属性?

C# 如何将单个属性映射到每个元素';使用AutoMapper的集合的属性?,c#,automapper,C#,Automapper,我想将Parent映射到ParentMapped,Child应该映射到ChildMapped,但是ChildMapped的源也是Parent,因为ChildMapped的SomeFlag属性应该由Parent的SomeFlag属性来源。 类如下所示: public class Parent { public bool SomeFlag { get; set; } public List<Child> Children { get; set; } } publ

我想将Parent映射到ParentMapped,Child应该映射到ChildMapped,但是ChildMapped的源也是Parent,因为ChildMapped的SomeFlag属性应该由Parent的SomeFlag属性来源。 类如下所示:

    public class Parent
{
    public bool SomeFlag { get; set; }
    public List<Child> Children { get; set; }
}

public class ParentMapped
{
    public List<ChildMapped> Children { get; set; }
}

public class Child
{
}

public class ChildMapped
{
    public bool SomeFlag { get; set; }
}
Mapper.CreateMap<Parent, ParentMapped>();
Mapper.CreateMap<Child, ChildMapped>();
Mapper.CreateMap<Parent,ChildMapped>()
      .ForMember(dest => dest.SomeFlag, opt => opt.MapFrom(src => src));
var instanceOfParent = new Parent();
var intanceOfParentMapped = mapper.Map<ParentMapped>(instanceOfParent);
公共类父类
{
公共bool SomeFlag{get;set;}
公共列表子项{get;set;}
}
公共类父映射
{
公共列表子项{get;set;}
}
公营儿童
{
}
公共类子类映射
{
公共bool SomeFlag{get;set;}
}
假设父类和子类的结构不能修改。我尝试如下配置映射:

    public class Parent
{
    public bool SomeFlag { get; set; }
    public List<Child> Children { get; set; }
}

public class ParentMapped
{
    public List<ChildMapped> Children { get; set; }
}

public class Child
{
}

public class ChildMapped
{
    public bool SomeFlag { get; set; }
}
Mapper.CreateMap<Parent, ParentMapped>();
Mapper.CreateMap<Child, ChildMapped>();
Mapper.CreateMap<Parent,ChildMapped>()
      .ForMember(dest => dest.SomeFlag, opt => opt.MapFrom(src => src));
var instanceOfParent = new Parent();
var intanceOfParentMapped = mapper.Map<ParentMapped>(instanceOfParent);
Mapper.CreateMap();
CreateMap();
Mapper.CreateMap()
.ForMember(dest=>dest.SomeFlag,opt=>opt.MapFrom(src=>src));
并通过如下方式调用来进行映射:

    public class Parent
{
    public bool SomeFlag { get; set; }
    public List<Child> Children { get; set; }
}

public class ParentMapped
{
    public List<ChildMapped> Children { get; set; }
}

public class Child
{
}

public class ChildMapped
{
    public bool SomeFlag { get; set; }
}
Mapper.CreateMap<Parent, ParentMapped>();
Mapper.CreateMap<Child, ChildMapped>();
Mapper.CreateMap<Parent,ChildMapped>()
      .ForMember(dest => dest.SomeFlag, opt => opt.MapFrom(src => src));
var instanceOfParent = new Parent();
var intanceOfParentMapped = mapper.Map<ParentMapped>(instanceOfParent);
var instanceOfParent=new Parent();
var intanceofparentmap=mapper.Map(instanceOfParent);
但此解决方案不起作用


使用AutoMapper是否也可以进行这种映射?

一种方法是将父属性添加到子属性,然后执行FormMember(x=>x.SomeFlag,opt=>opt.MapsFrom(src=>src.Parent.SomeFlag)


或者,您可以为父级使用ConstructUsing方法,但如果这样做,则使用AutoMapper没有什么意义。

您可以定义一个在映射之后运行的操作:

Mapper.CreateMap<Parent, ParentMapped>()
    .AfterMap((m,d) => d.Children.ForEach(c => c.SomeFlag = m.SomeFlag));
Mapper.CreateMap()
.AfterMap((m,d)=>d.Children.ForEach(c=>c.SomeFlag=m.SomeFlag));

谢谢,这是一种可能的方法,但源类无法修改。将此添加到问题中。我知道OP不想修改任何内容,但如果他想修改,该怎么办?修改ChildMapped.SomeFlag后,您将如何将其写回数据库?