How to use AutoMapper?

The first time using AutoMapper, and it's hard for me to determine how to use it. I am trying to map the ViewModel to my database tables.

My ViewModel looks like this:

public class AddressEditViewModel
{
    public AddressEdit GetOneAddressByDistrictGuid { get; private set; }
    public IEnumerable<ZipCodeFind> GetZipCodes { get; private set; }

    public AddressEditViewModel(AddressEdit editAddress, IEnumerable<ZipCodeFind> Zips)
    {
        this.GetOneAddressByDistrictGuid = editAddress;
        this.GetZipCodes = Zips;
    }
}   

The mapping I'm trying to use is ...

CreateMap<Address, AddressEditViewModel>();  

When I run this test ...

public void Should_map_dtos()
{
    AutoMapperConfiguration.Configure();
    Mapper.AssertConfigurationIsValid();
}  

I get this error ...

AutoMapper.AutoMapperConfigurationException: the following 2 properties on JCIMS_MVC2.DomainModel.ViewModels.AddressEditViewModel are not displayed: GetOneAddressByDistrictGuid GetZipCodes Add your own display expression, ignore or rename the property in JCIMSAdMMC2.MVC.

I am not sure how I should display these 2 properties. I would be grateful for any direction. Thanks

Mark

+5
1

, , , , .

-, AutoMapper diff. , viewmodel .

  • "Get...", .
  • , AutoSetter . .
  • , AutoMapper - . , .

    CreateMap<Address, AddressEditViewModel>()
             .ForMember( x => x.GetOneAddressByDistrictGuid , 
                              o => o.MapFrom( m => m."GetOneAddressByDistrictGuid") )
             .ForMember( x => x.GetZipCodes, 
                              o => o.MapFrom( m => m."GetZipCodes" ) );
    

Automapper DataObjects POCO View Model objects.

    public class AddressViewModel
    {
              public string FullAddress{get;set;}
    }

    public class Address
    {
              public string Street{get;set;}
              public string Suburb{get;set;}        
              public string City{get;set;}
    }

    CreateMap<Address, AddressViewModel>()
             .ForMember( x => x.FullAddress, 
                              o => o.MapFrom( m => String.Format("{0},{1},{2}"), m.Street, m.Suburb, m.City  ) );

    Address address = new Address(){
        Street = "My Street";
        Suburb= "My Suburb";
        City= "My City";
    };

    AddressViewModel addressViewModel = Mapper.Map(address, Address, AddressViewModel); 
+6

All Articles