个人资料:
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);
}
当我从数据库中获取记录时,我得到了一个不受支持的映射异常。没有返回记录时没有异常(表没有记录)
错误
缺少类型映射配置或不支持的映射。
映射类型: 学生 - &gt; StudentIndexModel ContosoUniversity.Models.Student - &gt; 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; }
}
我哪里错了?
答案 0 :(得分:0)
我已经完成了,我在2秒后找到了自己问题的答案。
我将此添加到Global.asax:
AutoMapperConfiguration.Configure();
AutomapperConfiguration
具有Mapper.Initialize()
方法