我有这个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(在上面的代码中的注释中有效)。我缺少什么?