AutoMapper 覆盖 EF Core 自动生成的属性

AutoMapper overriding EF Core auto-generated properties

我正在尝试在我的应用程序中使用时间戳来确定行的创建时间和最后修改时间。将 DTO 映射到实体时,属性将被覆盖并在它们最初具有值时设置为 null。 (注意我正在使用 CQRS 来处理命令)

这是我的基础实体,每个 EF Core 实体都继承了它

public class BaseEntity : IBaseEntity, IDeletable
{
    public int Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTimeOffset? DateCreated { get; set; }

    public string CreatedBy { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTimeOffset DateModified { get; set; } = DateTimeOffset.UtcNow;
    public bool IsDeleted { get; set; }
    public string DeletedBy { get; set; }
    public DateTimeOffset? DateDeleted { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }
}

用于请求的 DTO 是:

public sealed class UpdateCustomerCommand : IRequest<Result<int>>
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string PrimaryContact { get; set; }
    public string SecondaryContact { get; set; }
    public ICollection<AddressCreateDto> Addresses { get; set; }
}

请注意,我没有包含 BaseEntity 中的属性,因为 EF Core 应该自动生成这些值,我认为在发出请求时任何人都不需要担心名为 DateCreated 的 属性等等……这只是为了审计目的

var repository = _unitOfWork.GetRepository<Customer>();
var customer = await repository.FindAsync(request.Id, cancellationToken);
if (customer == null) throw new KeyNotFoundException();
var mappedCustomer = _mapper.Map<Customer>(request);
await repository.UpdateAsync(request.Id, mappedCustomer);

当我使用 FindAsync(request.Id, cancellationToken) 方法检索对象时,值在那里,但在我进行映射后,它们被覆盖了。

这是映射。

CreateMap<UpdateCustomerCommand, Customer>()
.ForMember(dst => dst.Addresses, opt => opt.MapFrom(src => src.Addresses))
.ReverseMap();

如果您不希望映射这些属性,则必须在创建映射时明确说明。根据您使用的 AutoMapper 版本,您需要忽略映射或使用 DoNotValidate。

// Old
cfg.CreateMap<Source, Dest>()
    .ForMember(dest => dest.DateModified, opt => opt.Ignore());

// New
cfg.CreateMap<Source, Dest>()
    .ForMember(dest => dest.DateModified, opt => opt.DoNotValidate());

这是一个link to the documentation