【问题标题】:Automapper missing type map configuration or unsupported mapping (automapper 6.1.1)Automapper 缺少类型映射配置或不支持的映射(automapper 6.1.1)
【发布时间】:2018-11-15 19:55:19
【问题描述】:

这就是我正在尝试做的事情并在这一行遇到异常:

Mapper.Map<CreditCard>(cardVM);

虽然其他映射工作正常,但这两个实体却不行 这是两者的模型:

[Serializable]
    public class CreditCard : BaseEntity
    {
        public long UserId { get; set; }
        public string BankToken { get; set; }
        public string CardNumber { get; set; }
        public User User { get; set; }
    }


[Serializable]
    public class CreditCardVM
    {
        public Guid? UID { get; set; }
        public long UserId { get; set; }
        public string BankToken { get; set; }
        public string CardNumber { get; set; }
        public string PaymentMethodUId { get; set; }
        public User User { get; set; } 
    }

但仍然出现异常“Automapper 缺少类型映射配置或不支持的映射”

【问题讨论】:

  • 您好,您可以通过完整的错误和堆栈跟踪吗?
  • 完整的错误信息告诉你什么不能被映射。
  • 你能发布确切的内部异常
  • 谢谢,但我得到了解决方案,我缺少必须使用 Automapper.Profile 继承的 Createmap

标签: c# object exception mapping automapper


【解决方案1】:

检查 Automapper 配置启动怎么样,也许你的映射配置没有初始化。

因此,请检查启动项目中的应用程序启动配置,例如在 Web 应用程序中,您必须检查“Application_start in Global.asax”

protected void Application_Start()
{
    AutoMapperConfigurator.Initialize();
}

【讨论】:

    【解决方案2】:

    谢谢大家,但这是我错过的。

    public class CreditCardAutoMapperProfile : Profile
        {
            public CreditCardAutoMapperProfile()
            {
                CreateMap<Data.Entities.CreditCard, CreditCardVM>();
            }
        }
    

    它开始工作了:)

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2017-04-16
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多