摘要 类 映射派生类型

Abstract Classes Mapping Derived Types

我有以下摘要 class 及其实现:

public abstract class TransactionResult : AuditableEntity
{
    public int Id { get; set; }
    [Required, MaxLength(25)]
    public string Status { get; set; }

    [Required, MaxLength(50)]
    public string ReferenceId { get; set; }

    public string ResultMetaData { get; set; }

    [Required, MaxLength(10)]
    public string CardType { get; set; }
    [Required, MaxLength(10)]
    public string CardDescription { get; set; }
    public int? CustomerSavedCardId { get; set; }
    public CustomerSavedCard CustomerSavedCard { get; set; }

    public int? PaymentPlanDetailId { get; set; }
    public PaymentPlanDetail PaymentPlanDetail { get; set; }

    public int? CustomerMembershipBillingId { get; set; }
    public CustomerMembershipBilling CustomerMembershipBilling { get; set; }

    public Guid? NotificationId { get; set; }
    
  
    public Notification Notification { get; set; }


    [Required, MaxLength(25)]
    public string Source { get; set; }

    public string CustomerKey { get; set; }
    public Customer Customer { get; set; }


    public int Amount { get; set; }
}

public class Approve : TransactionResult
{

}

public class Decline : TransactionResult
{
    public string Reason { get; set; }
}

我在使用自动映射器时试图找出原因

 profile.CreateMap<Entities.TransactionResult, TransactionsDto>()
                .ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.Customer.FirstName + " " + src.Customer.LastName))
                .ForMember(dest => dest.NotificationId, opt => opt.MapFrom(src => src.NotificationId))
                .ForMember(dest => dest.Reason, opt => opt.MapFrom(src => src.GetType() == typeof(Entities.Decline) ? ((Entities.Decline)src).Reason : null))

我在将拒绝转换添加到交易结果时遇到错误。

The client projection contains a reference to a constant expression of 'System.RuntimeType'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information.

Automapper 支持继承。

删除 属性 Reason 的行并为 Decline 创建一个特定的配置文件,如下所示:

profile.CreateMap<Entities.Decline, TransactionsDto>()
    .IncludeBase<Entities.TransactionResult, TransactionsDto>();