简体   繁体   English

ASP.NET Core AutoMapper 配置错误

[英]ASP.NET Core AutoMapper configuration error

I am trying to map an entity with a model but it's giving an error how can I check which property is causing the error?我正在尝试 map 一个带有 model 的实体,但它给出了一个错误,我如何检查哪个属性导致了错误?

Error: AutoMapper.AutoMapperMappingException: 'Error mapping types.'错误:AutoMapper.AutoMapperMappingException:“错误映射类型。” AutoMapperMappingException: Missing type map configuration or unsupported mapping. AutoMapperMappingException:缺少类型 map 配置或不支持的映射。
This exception was originally thrown at this call stack:此异常最初是在此调用堆栈中抛出的:

AutoMapper.Mapper.MapCore<TSource, TDestination>(TSource, TDestination, AutoMapper.ResolutionContext, System.Type, System.Type, AutoMapper.MemberMap) AutoMapper.Mapper.MapCore<TSource, TDestination>(TSource, TDestination, AutoMapper.ResolutionContext, System.Type, System.Type, AutoMapper.MemberMap)
AutoMapper.Mapper.AutoMapper.IInternalRuntimeMapper.Map<TSource, TDestination>(TSource, TDestination, AutoMapper.ResolutionContext, System.Type, System.Type, AutoMapper.MemberMap) AutoMapper.Mapper.AutoMapper.IInternalRuntimeMapper.Map<TSource, TDestination>(TSource, TDestination, AutoMapper.ResolutionContext, System.Type, System.Type, AutoMapper.MemberMap)
AutoMapper.ResolutionContext.MapInternal<TSource, TDestination>(TSource, TDestination, AutoMapper.MemberMap) AutoMapper.ResolutionContext.MapInternal<TSource, TDestination>(TSource, TDestination, AutoMapper.MemberMap)

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<Account, AccountModel>();
        CreateMap<AccountModel, Account>();

        CreateMap<ServicePlan, ServicePlanModel>()
              .ForMember(ServicePlanModel => ServicePlanModel.ServicePlanDetails, opt => opt.MapFrom(src => src.ServicePlanDetails.ToList()))
              .ForMember(dest => dest.Lines, opt => opt.MapFrom(src => src.NLines ?? default))
              .ForMember(dest => dest.StartDate, opt => opt.MapFrom(src => src.DateStart))
              .ForMember(dest => dest.EndDate, opt => opt.MapFrom(src => src.DateEnd))
              .ForMember(dest => dest.MonthlyCharges, opt => opt.MapFrom(src => src.AmountMonthly))
              .ForMember(dest => dest.OneTimeCharges, opt => opt.MapFrom(src => src.AmountOnetime));
    }
}

public async Task<List<ServicePlanModel>> GetServicePlansWithDetails(long accountId)
{
    var plan = mapper.Map<ServicePlan, ServicePlanModel>(servicePlan);
}

public partial class ServicePlan
{
    public ServicePlan()
    {
        ServicePlanDetails = new HashSet<ServicePlanDetail>();
    }

    public long PlanId { get; set; }
    public decimal PhoneId { get; set; }
    public byte? NLines { get; set; }
    public DateTime? DateStart { get; set; }
    public DateTime? DateEnd { get; set; }
    public decimal? AmountMonthly { get; set; }
    public decimal? AmountOnetime { get; set; }
    public string? ContractType { get; set; }
    public string? CallerId { get; set; }
    public bool? Active { get; set; }
    public short? ResellerId { get; set; }
    public int? OnetimeInvoiceNo { get; set; }
    public bool? IsRequest { get; set; }
    public string? RequestType { get; set; }
    public string? NewValue { get; set; }
    public DateTime? RequestDate { get; set; }
    public string? RequestBy { get; set; }
    public bool? IsRejected { get; set; }
    public bool? IsApproved { get; set; }
    public DateTime? ApprovedWhen { get; set; }
    public string? ApprovedBy { get; set; }
    public string? Comments { get; set; }
    public string? AdditionalDetails { get; set; }
    public string? VideoTronId { get; set; }
    public string? Zone { get; set; }
    public string? PrevProvider { get; set; }
    public bool? Charge911Tax { get; set; }
    public string? ContractTerms { get; set; }
    public long? ContractId { get; set; }
    public int AllowedBandwidth { get; set; }
    public DateTime? ServiceStartDate { get; set; }
    public string? AppointmentTime { get; set; }
    public DateTime? AppointmentEmailSentOn { get; set; }
    public string? TrackingNumber { get; set; }
    public bool? HasNumberPort { get; set; }
    public DateTime? EstPortDate { get; set; }
    public DateTime? PortCompletedOn { get; set; }
    public bool? IsExGbchargesLimited { get; set; }
    public decimal? ExGbchagesLimitAmt { get; set; }
    public bool? IsPreVisitSch { get; set; }
    public DateTime? PreVisitSchDate { get; set; }
    public string? PreVisitSchTime { get; set; }
    public bool? MoniterBwUsage { get; set; }
    public int? BwWarnLimit { get; set; }
    public string? DslaccountId { get; set; }
    public string? BellGisId { get; set; }
    public string? DslPassword { get; set; }
    public bool? IsPasswordPassed { get; set; }
    public bool? IsDslUserPassed { get; set; }
    public string? CogecoId { get; set; }
    public int? ServiceProviderId { get; set; }
    public string? RogersId { get; set; }
    public decimal? InternetSpeed { get; set; }
    public bool? ModemRental { get; set; }
    public bool? RouterRental { get; set; }
    public bool? Atarental { get; set; }
    public string? DslaccountDomain { get; set; }

    [NotMapped]
    public virtual ICollection<ServicePlanDetail> ServicePlanDetails { get; set; }
}

public class ServicePlanModel
{
    public ServicePlanModel()
    {
        ServicePlanDiscounts = new List<ServicePlanDiscount>();
        ServicePlanDetails = new List<ServicePlanDetail>();
    }

    [Key]
    public Int64 PlanId { get; set; }
    public Int64? ContractId { get; set; }
    public int ResellerId { get; set; }
    public Int64 PhoneId { get; set; }
    public bool IsPhone { get; set; }
    public Int32? OneTimeInvoiceId { get; set; }
    public UInt16 Lines { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public Decimal MonthlyCharges { get; set; }
    public Decimal OneTimeCharges { get; set; }
    public String ContractType { get; set; }
    public String CallerId { get; set; }
    public Boolean IsActive { get; set; }
    public Boolean IsRequest { get; set; }
    public Boolean IsRejected { get; set; }
    public Boolean IsApproved { get; set; }
    public Boolean Charge911Tax { get; set; }
    public DateTime ApprovedOn { get; set; }
    public String ApprovedBy { get; set; }
    public String Comments { get; set; }
    public String ContractTerms { get; set; }
    public String AdditionalDetails { get; set; }
    public String VideoTronId { get; set; }
    public String Zone { get; set; }
    public String PreviousProvider { get; set; }
    public String RequestType { get; set; }
    public String NewValue { get; set; }
    public DateTime? RequestDate { get; set; }
    public String RequestedBy { get; set; }
    //public PhoneNumber PhoneNumber { get; set; }
    public List<ServicePlanDetail> ServicePlanDetails { get; set; }
    public List<ServicePlanDiscount> ServicePlanDiscounts { get; set; }

    public bool IsLandLinePlan
    {
        get
        {
            if (ServicePlanDetails != null)
            {
                return ServicePlanDetails.Any(spd => new String[] { "landline-r", "landline-b", "plan_10103", "plan_10104", "plan_10105" }.Contains(spd.ServiceCode));
            }

            return false;
        }
    }

    public String ServicePlanName
    {
        get
        {
            if (ServicePlanDetails != null && ServicePlanDetails.Count > 0)
            {
                var plan = ServicePlanDetails.SingleOrDefault(spd => spd.IsPlan);
                if (plan != null)
                {
                    if (IsPhone)
                    {
                        return String.Format("{0} ({3}) ({1} x {2})", plan.ServiceTitle, plan.UnitPrice.ToString("N2"), plan.Quantity, PhoneId.ToString("###-###-####"));
                    }

                    return String.Format("{0} ({1} x {2})", plan.ServiceTitle, plan.UnitPrice.ToString("N2"), plan.Quantity);
                }
            }
            return String.Empty;
        }
    }

    public String MonthlyChargesDetails
    {
        get
        {
            if (ServicePlanDetails != null && ServicePlanDetails.Count > 0)
            {
                var items = ServicePlanDetails.Where(spd => spd.IsPlan == false & spd.UnitPrice > 0 & spd.ServiceType.Equals(Constants.SERVICE_TYPE_MONTHLY)).ToList();

                var details = new StringBuilder();

                foreach (var spd in items)
                {
                    details.Append(String.Format("{0} ({1} x {2}) ", spd.ServiceTitle, spd.UnitPrice.ToString("N2"),
                                                 spd.Quantity));
                }

                return details.ToString();
            }

            return String.Empty;
        }
    }

    //public String OnetimeChargesDetails
    //{
    //    get
    //    {
    //        if (ServicePlanDetails != null && ServicePlanDetails.Count > 0)
    //        {
    //            var items = ServicePlanDetails.Where(spd => spd.IsPlan == false & spd.UnitPrice > 0 & spd.ServiceType.Equals(Constants.SERVICE_TYPE_ONETIME)).ToList();

    //            var details = new StringBuilder();
    //            foreach (var spd in items)
    //            {
    //                details.Append(String.Format("{0} ({1} x {2}) ", spd.ServiceTitle, spd.UnitPrice.ToString("N2"),
    //                                             spd.Quantity));
    //            }
    //            return details.ToString();
    //        }
    //        return String.Empty;
    //    }
    //}

    public List<ServicePlanDetail> OnetimeChargesDetails
    {
        get
        {
            if (ServicePlanDetails != null && ServicePlanDetails.Count > 0)
            {
                return ServicePlanDetails.Where(spd => spd.IsPlan == false & spd.ServiceType.Equals(Constants.SERVICE_TYPE_ONETIME)).ToList();
            }

            return null;
        }
    }

    public List<ServicePlanDetail> FreeServicesDetails
    {
        get { return ServicePlanDetails != null ? ServicePlanDetails.Where(s => s.UnitPrice == 0 && !s.ServiceType.Equals("month")).ToList() : null; }
    }

    public List<ServicePlanDetail> DiscountedServicesDetail
    {
        get
        {
            return ServicePlanDetails != null ?
            ServicePlanDetails.Where(s => s.ResellerService != null && s.UnitPrice < s.ResellerService.SuggestedPrice && !s.ServiceType.Equals("month")).ToList() : null;
        }
    }

    public string Modem_MAC { get; set; }
    public string TrackingNumber { get; set; }
}

I tried debugging but could not find the error.我尝试调试但找不到错误。

Take a look at the source for AutoMapperMappingException .查看AutoMapperMappingException 的源代码 There are a number of properties on the exception that can be examined while under a debugger.在调试器下可以检查异常的许多属性。

From the source...从源头...

public TypePair? Types { get; set; }
public TypeMap TypeMap { get; set; }
public MemberMap MemberMap { get; set; }

These properties are used to create the string for the Message property.这些属性用于为Message属性创建字符串。

If for some reason that doesn't give you enough information, a way more intensive and semi-destructive way (have a backup of your source) is to cut half of the properties from the source/target types and see if the error still occurs.如果由于某种原因没有给你足够的信息,一种更密集和半破坏性的方法(备份你的源)是从源/目标类型中削减一半的属性,看看错误是否仍然发生. If yes, cut in half again, if no take the other properties and try again.如果是,再减半,如果不是,取其他属性再试一次。

Basically, you are doing a binary search to narrow down the problem until it becomes obvious.基本上,您正在进行二进制搜索以缩小问题范围,直到它变得明显为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM