【问题标题】:Missing mapping configuration, classes mapped in profile缺少映射配置,配置文件中映射的类
【发布时间】:2015-12-04 03:40:36
【问题描述】:

简介:

public class StudentProfile : Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<Student, StudentIndexModel>();
    }
}

以及它中断的行:

public ActionResult Index()
{
    // Breaks here
    var students = Mapper.Map<IEnumerable<StudentIndexModel>>(db.Students.ToList());

    return View(students);
}

当它从数据库中获取记录时,我遇到了一个不受支持的映射异常。没有返回记录时也不例外(表没有记录)

错误

缺少类型映射配置或不支持的映射。

映射类型: 学生 -> StudentIndexModel ContosoUniversity.Models.Student -> ContosoUniversity.ViewModels.StudentIndexModel

目标路径: IEnumerable`1[0]

学生班级:

public class Student : Entity
{
    public Student() { }

    public string LastName { get; set; }

    public string Forenames { get; set; }

    public DateTime EnrolmentDate { get; set; }

    public virtual ICollection<Enrolment> Enrolments { get; set; }
}

还有 StudentIndexModel 类:

public class StudentIndexModel
{
    public int Id { get; set; }

    public string LastName { get; set; }

    public string Forenames { get; set; }

    public DateTime EnrolmentDate { get; set; }
}

我哪里错了?

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    我又做了一次,2秒后我找到了自己问题的答案。

    我将此添加到 Global.asax:

    AutoMapperConfiguration.Configure();
    

    AutomapperConfigurationMapper.Initialize() 方法的地方

    【讨论】:

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