automapper merge,不能忽略child的属性

时间:2018-06-13 21:08:27

标签: c# automapper

我有这个POCO:

public class Product
{
  public int Id { get; set; }
  public virtual ICollection<ProductLocalization> Localizations { get; set; }
}

public class ProductLocalization
{
  public int Id { get; set; }
  public int ProductId { get; set; }
}

我想合并两个产品,但我想保留旧的ProductLocalization.Id。所以我这样做了:

internal class ProductProfile : Profile
{
    public ProductProfile()
    {
        CreateMap<ProductLocalization, ProductLocalization>()
            .ForMember(d => d.Id, opt => opt.Ignore());
            //.AfterMap((s, d) => d.Id = 5);

        CreateMap<Product, Product>();
    }
}

var mapper = new AutoMapperServiceConfiguration().CreateConfiguration().CreateMapper();
mapper.Map(sourceProduct, destProduct);

这不起作用(复制了新的ProductLocalization.Id值,但AfterMap(在上面的代码中的注释中有效)。我缺少什么?

0 个答案:

没有答案