抽象类映射派生类型

时间:2021-05-14 19:27:47

标签: entity-framework-core automapper

我有以下抽象类及其实现:

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))

向交易结果添加拒绝转换时出现错误。

<块引用>

客户端投影包含对“System.RuntimeType”常量表达式的引用。这可能会导致内存泄漏;考虑将此常量分配给局部变量并在查询中使用该变量。有关详细信息,请参阅 https://go.microsoft.com/fwlink/?linkid=2103067

1 个答案:

答案 0 :(得分:1)

Automapper 支持继承。

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

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