C# 自动映射嵌套映射不工作。以下是配置和代码:

C# 自动映射嵌套映射不工作。以下是配置和代码:,c#,automapper,C#,Automapper,按照Automapper wiki中提到的步骤配置具有复杂对象的嵌套映射。但不起作用: public class Student { public int ID{get;set;} public string Name { get; set; } public string Standard { get; set; } public List<string> Course { get; set; }

按照Automapper wiki中提到的步骤配置具有复杂对象的嵌套映射。但不起作用:

public class Student
    {
        public int ID{get;set;}
        public string Name { get; set; }
        public string Standard { get; set; }
        public List<string> Course { get; set; }
        public int Age { get; set; }
        public string FatherName { get; set; }
        public string  MotherName{ get; set; }
        public char Gender { get; set; }
        public DateTime DOB { get; set; }
        public string BloodGroup { get; set; }
        public int TestCondition { get; set; }
        public Address Address { get; set; }

    }

  public class Address
    {
        public int ID { get; set; }
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string Zip { get; set; }
    }
配置:

public class AutoMapperProfileConfig : Profile
    {

        public AutoMapperProfileConfig()
        {

            CreateMap<Student, StudentDTO>().ForMember(dest => dest.DOB, opt=>opt.Ignore())
                                            .ForMember(x=>x.BG,opt=>opt.MapFrom(y=>y.BloodGroup))/*.AfterMap((src, dest) => dest.Name = "After MAP")*/
                                            .ForMember(x=>x.TestCondition, opt=>opt.Condition(src => (src.TestCondition >= 100))
                                            );
CreateMap<Address, AddressDTO>();
public类AutoMapperProfileConfig:Profile
{
公共自动映射配置文件()
{
CreateMap().formMember(dest=>dest.DOB,opt=>opt.Ignore())
.ForMember(x=>x.BG,opt=>opt.MapFrom(y=>y.BloodGroup))/*.AfterMap((src,dest)=>dest.Name=“映射后”)*/
.ForMember(x=>x.TestCondition,opt=>opt.Condition(src=>(src.TestCondition>=100))
);
CreateMap();
执行:

Student student = new Student();
            Address add = new Address();
            add.Line1 = "abcd";
            student.Address = add;
            var studentLite = Mapper.Map<StudentDTO>(student);
Student=newstudent();
地址添加=新地址();
add.Line1=“abcd”;
student.Address=add;
var studentLite=Mapper.Map(学生);

虽然studentDTO映射正确,但AddressDTO为空。

您正在将
Student.Address
映射到
studentDTO.AddressDTO
。由于目标属性的名称已更改,AutoMapper不知道如何处理它。如果您将
studentDTO
中的propertyname从
AddressDTO
更改为
地址
,Automapper应该能够提取并映射它


当然,另一种解决方案是明确指示AutoMapper将
Student.Address
映射到
StudentDTO.AddressDTO
您正在将
Student.Address
映射到
StudentDTO.AddressDTO
。由于目标属性的名称已更改,AutoMapper不知道如何处理它。如果您更改将
studentdt中的属性名称从
AddressDTO
导入
Address
,Automapper应该能够提取并映射它


当然,另一种解决方案是明确指示AutoMapper将
Student.Address
映射到
StudentDTO.AddressDTO

我使用了CreateMap,它只创建从类地址到AddressDTO的映射。我指的是您的属性名称。AutoMapper只会自动映射名称相同的属性。您的不完全相同,因此您必须告诉AutoMapper如何映射它们。@DirkTrilsbeek,谢谢。这很有帮助。为什么我自己没有想到这个解释。我使用了CreateMap,它只是创建了从类地址到AddressDTO的映射。我指的是您的属性名称。AutoMapper只会自动映射已命名的属性完全相同。你的不完全相同,因此你必须告诉AutoMapper如何绘制它们。@DirkTrilsbeek,谢谢你。这很有帮助。为什么我自己没有想到这个解释呢。
Student student = new Student();
            Address add = new Address();
            add.Line1 = "abcd";
            student.Address = add;
            var studentLite = Mapper.Map<StudentDTO>(student);