我在ASP.NET MVC 5应用程序中使用AutoMapper 6.2.0.
当我通过控制器调用我的视图时,它显示所有正确的事情.但是,当我刷新该视图时,Visual Studio显示错误:
System.InvalidOperationException:'Mapper已初始化.您必须为每个应用程序域/进程调用一次Initialize.
我只在一个控制器中使用AutoMapper.尚未在任何地方进行任何配置,也未在任何其他服务或控制器中使用AutoMapper.
我的控制器:
public class StudentsController : Controller
{
private DataContext db = new DataContext();
// GET: Students
public ActionResult Index([Form] QueryOptions queryOptions)
{
var students = db.Students.Include(s => s.Father);
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Student, StudentViewModel>();
});
return View(new ResulList<StudentViewModel> {
QueryOptions = queryOptions,
Model = AutoMapper.Mapper.Map<List<Student>,List<StudentViewModel>>(students.ToList())
});
}
// Other Methods are deleted for ease...
Run Code Online (Sandbox Code Playgroud)
控制器内出错:
我的Model类:
public class Student
{
[Key]
public int Id { get; set; }
public string Name { get; …Run Code Online (Sandbox Code Playgroud)