开发者

How to make AutoMapper create an instance of class

开发者 https://www.devze.com 2023-04-08 21:31 出处:网络
I have the following source type: public class Source { public string FirstName { get; set; } public string LastName { get; set; }

I have the following source type:

public class Source
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

I have the following destination types:

public class Destination
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; se开发者_StackOverflow社区t; }
    public string PostalCode { get; set; }
}

I created a mapping:

Mapper.CreateMap<Source, Destination>();

How do I configure my mapping so it will create an instance of Address and map the Address.PostalCode property using the Source property ZipCode?


With AfterMap, you can specify how to map entities further after AutoMapper has done it's mapping.

Mapper.CreateMap<Source, Destination>()
                .AfterMap((src, dest) =>
                              {
                                  dest.HomeAddress = new Address {PostalCode = src.ZipCode};
                              }
            );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号