缺少映射配置,类 在配置文件中映射

Missing mapping configuration, classes mapped in profile

简介:

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

从数据库中获取记录时出现不支持的映射异常。没有记录返回时也不例外(table没有记录)

错误

Missing type map configuration or unsupported mapping.

Mapping types: Student -> StudentIndexModel ContosoUniversity.Models.Student -> ContosoUniversity.ViewModels.StudentIndexModel

Destination path: IEnumerable`1[0]

学生class:

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 class:

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

    public string LastName { get; set; }

    public string Forenames { get; set; }

    public DateTime EnrolmentDate { get; set; }
}

我哪里错了?

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

我将此添加到 Global.asax:

AutoMapperConfiguration.Configure();

其中 AutomapperConfigurationMapper.Initialize() 方法