自动映射器不会映射所有已配置的字段

时间:2018-09-03 16:11:29

标签: c# .net automapper

我有一个数据库对象,我想映射到我的应用程序中的视图对象,但并不是每个属性都被映射。

这是我的Automapper设置:

这是我希望AutoMapper映射到

的View类
public class CustomerDetails
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Dob { get; set; }
    public DateTime CreateDate { get; set; }

    public decimal Balance { get; set; }

    public List<Email> Emails { get; set; }
    public List<Address> Addresses { get; set; }


public class Email
{
        public Guid Id { get; set; }
        public string EmailName { get; set; }
}

  public class Address
    {
        public Guid Id { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
    }

}

这是数据库类:

    public class Customer
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }

            public virtual CustomerBalance Balance { get; set; }
            public virtual ICollection<Email> Emails { get; set; }
            public virtual ICollection<Address> Address { get; set; }

    public class Email
    {
        public Guid Id { get; set; }
        public string EmailName { get; set; }
        public bool IsPrimary { get; set; }
        public virtual Customer Customer { get; set; }

    }

 public class Address
    {
        public Guid Id { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Country { get; set; }
    }
    }

这是我为AutoMapper设置配置的方式

  Mapper.Initialize(config =>
            {
                config.AddProfile<CustomerProfile>();
            }); 


  public class CustomerProfile : Profile
    {

        public CustomerProfile()
        {
            CreateMap<DataModels.Phone, Phone>();
            CreateMap<DataModels.Email, Email>();
            CreateMap<DataModels.Address, Address>();
            CreateMap<DataModels.CustomerPin, Pin>()
                .ForMember(x => x.PinNumber, y => y.MapFrom(s => s.Pin))
                .ForMember(x => x.Id, y => y.MapFrom(s => s.Id))
                ;

            CreateMap<Customer, CustomerDetails>()
                .ForMember(x => x.Phones, y => y.MapFrom(s => s.Phones))
                .ForMember(x => x.Emails, y => y.MapFrom(s => s.Emails))
                .ForMember(x => x.Balance, y => y.MapFrom(s => s.Balance.Balance))
                .ForMember(x => x.Pins, y => y.MapFrom(s => s.Pin))
                .ForMember(x => x.Addresses, y => y.MapFrom(s => s.Address))

                ;

            CreateMap<Customer, CustomerDetails>().ReverseMap();
        }
    }

奇怪的是,除了Addresses的属性和Balance上的CustomerDetails.cs以外,所有内容都按预期映射。即使我指定要从成员映射它,列表集合也为null。但是,电子邮件列表已正确映射。

我想念什么吗?

0 个答案:

没有答案