【问题标题】:Abstract Classes Mapping Derived Types抽象类映射派生类型
【发布时间】:2021-08-04 23:04:45
【问题描述】:

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

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 了解更多信息。

【问题讨论】:

    标签: entity-framework-core automapper


    【解决方案1】:

    Automapper 支持继承。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2011-02-01
      • 1970-01-01
      相关资源
      最近更新 更多