我有以下架构:
现在我正在处理一对多关系(例如:发票可以有多个职位)。
数据转换对象:
public class InvoiceDto
{
public string Number { get; set; }
public DateTime CreationDate { get; set; }
public List<InvoicePositionDto> InvoicePositions { get; set; }
}
public class InvoicePositionDto
{
public string Description { get; set; }
public decimal Costs { get; set; }
}
实体对象:
[Table("Invoices")]
public class InvoiceEntity : BaseEntity
{
[Required]
public string Number { get; set; }
[Required]
public DateTime CreationDate { get; set; }
public virtual ICollection<InvoicePositionEntity> InvoicePositions { get; set; }
}
[Table("InvoicePosition")]
public class InvoicePositionEntity : BaseEntity
{
[Required]
public string Description { get; set; }
[Required]
public decimal Costs { get; set; }
[Required]
public int InvoiceId { get; set; }
public virtual InvoiceEntity Invoice { get; set; }
}
创建发票时,我只会在数据访问层上插入所有位置。
更新发票时,我必须考虑以下有关发票头寸的情况:
我该如何处理这些案件?我想我总是要将InvoicePositionDtos
与现有数据集的InvoicePositionEntities
进行“比较”。有更简单的方法吗?
技术细节 - 使用以下框架:
答案 0 :(得分:-1)
您是否尝试过以下内容?
AutoMapper.Mapper.CreateMap<InvoiceDto, InvoiceEntity>()
.ForMember(dest => dest.InvoicePositions, opts => opts.MapFrom(src => src.InvoicePositions.Select(x => AutoMapper.Mapper.Map<InvoiceEntity>(x)).ToList()));