简体   繁体   中英

AutoMapper - How to Map to three level deep

I'm trying to user AutoMapper to flatten a Entity with relation to another Entity which has relation to third Entity to view model

How to map these three entities into one?

Source:

public class Address
{
    public int AddressId { get; set; }
    public string AddressLine1 { get; set; }       
    public int CityId { get; set; }
    public virtual City City { get; set; }  
}

public class City
{
    public int CityId { get; set; }
    public string CityName { get; set; }
    public int CountryId { get; set; }
    public virtual Country Country { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
}
public class Country
{
    public int CountryId { get; set; }
    public string CountryName { get; set; }
    public virtual ICollection<City> Cities { get; set; }
}

Destination:

Public Class AddressViewModel
{
    public int AddressId { get; set; }
    public string AddressLine1 { get; set; }       
    public int CityId { get; set; }
    public string CityName { get; set; }
    public int CountryId { get; set; }
    public string CountryName { get; set}
}

A couple of ways (at least). If you name your viewmodel fields differently it can happen by convention:

Public Class AddressViewModel
{
    public int AddressId { get; set; }
    public string AddressLine1 { get; set; }       
    public int CityCityId { get; set; }
    [DisplayName("City Name")]
    public string CityCityName { get; set; }
    public int CityCountryCountryId { get; set; }
    [DisplayName("Country Name")]
    public string CityCountryCountryName { get; set}
}

If that's too ugly, you can do it in CreateMap:

Mapper.CreateMap<Address, AddressViewModel>()
    .ForMember(dest => dest.CityId, opts => opts.MapFrom(src => src.City.CityId))
    .ForMember(dest => dest.CityName, opts => opts.MapFrom(src => src.City.CityName))
    .ForMember(dest => dest.CountryId, opts => opts.MapFrom(src => src.City.Country.CountryId))
    .ForMember(dest => dest.CountryName, opts => opts.MapFrom(src => src.City.Country.CountryName));

http://automapper.codeplex.com/wikipage?title=Flattening&referringTitle=Home

Example to use Include() to query data in deeper levels:

    public AddressViewModel GetAddressById(int id)
    {
        var result = applicationDbContext.Address
            .Include(o=>o.City)
            .Include(o=>o.City.Country)
            .FirstOrDefault(x=>x.AddressId == id);
        return mapper.Map<AddressViewModel>(result);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM