title: AutoMapper version 10 and ASP.NET MVC
body: <p>I have an application I haven't touched for a while and it is giving me some grief.</p>
  <p>When I call the index method of the controller I get the following error:</p>
  <p><a href="https://i.stack.imgur.com/74oWS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/74oWS.png" alt="enter image description here" /></a></p>
  <p>I am not sure what I am doing wrong but it would seem that AutoMapper is having trouble mapping a collection of <code>Shift</code> objects to a <code>ShiftViewModel</code>.</p>
  <p>I have included some snippets below.</p>
  <p>Thoughts?</p>
  <p>My controller:</p>
  <pre><code>using AutoMapper;
  using My.DataAccess;
  using My.Entity.DatabaseEntities;
  using My.Entity.ViewModels;
  using My.Service;
  using System.Net;
  using System.Threading.Tasks;
  using System.Web.Mvc;
  
  namespace My.Controllers
  {
      public class ShiftController : Controller
      {
          //initialize service object
          readonly IShiftService _shiftService;
          private readonly IMapper _mapper;
  
          public ShiftController(IShiftService shiftService, IMapper mapper)
          {
              _shiftService = shiftService;
              _mapper = mapper;
          }
  
          readonly ApplicationDataManager db = new ApplicationDataManager();
  
          // GET: /Shifts/
          public ActionResult Index()
          {
              var shifts = _shiftService.GetAll();
  
              if (shifts == null)
              {
                  return HttpNotFound();
              }
  
  
              var model = _mapper.Map&lt;ShiftViewModel&gt;(shifts);
  
              return View(model);
          }
      }
  }
  </code></pre>
  <p><code>Shift</code> database entity:</p>
  <pre><code>using System;
  
  using System.ComponentModel.DataAnnotations;
  
  namespace My.Entity.DatabaseEntities
  {
      public class Shift : AuditableEntity&lt;long&gt;
      {
          [Required, StringLength(6)]
          [Editable(true)]
          public string Code { get; set; }
  
          public string Detail { get; set; }
          public DateTime Start { get; set; }
          public long Duration { get; set; }
      }
  }
  </code></pre>
  <p><code>ShiftViewModel</code> class:</p>
  <pre><code>using System;
  using System.ComponentModel.DataAnnotations;
  
  namespace My.Entity.ViewModels
  {
      public class ShiftViewModel
      {
          public int Id { get; set; }
          public string Code { get; set; }
          public string Detail { get; set; }
          public DateTime Start { get; set; }
  
          [Display(Name = &quot;Duration of shift&quot;)]
          [DisplayFormat(DataFormatString = &quot;{0:HH:mm}&quot;, ApplyFormatInEditMode = true)]
          [DataType(DataType.Duration)]
          public string DurationTime
          {
              get
              {
                  var ts = new TimeSpan(Duration);
                  var h = ts.Hours == 1 ? &quot;hour&quot; : &quot;hours&quot;;
                  var m = ts.Minutes == 1 ? &quot;min&quot; : &quot;mins&quot;;
                  return string.Format(&quot;{0} {1} {2} {3}&quot;, ts.Hours, h, ts.Minutes, m);
              }
          }
          public long Duration { get; set; }
      }
  }
  </code></pre>
  <p><code>Global.asax</code>:</p>
  <pre><code>using Autofac;
  using Autofac.Integration.Mvc;
  using My.DataAccess.Modules;
  using System.Web.Mvc;
  using System.Web.Optimization;
  using System.Web.Routing;
  
  namespace My.App
  {
      public class MvcApplication : System.Web.HttpApplication
      {
          protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
  
              //Autofac Configuration
              var builder = new ContainerBuilder();
  
              builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();
  
              builder.RegisterModule(new RepositoryModule());
              builder.RegisterModule(new ServiceModule());
              builder.RegisterModule(new EFModule());
  
              //Register AutoMapper here using AutoFacModule class (Both methods works)
              //builder.RegisterModule(new AutoMapperModule());
              builder.RegisterModule&lt;AutoFacModule&gt;();
  
              var container = builder.Build();
  
              DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
          }
      }
  }
  </code></pre>
  <p><code>AutoFacModule</code>:</p>
  <pre><code>using Autofac;
  using AutoFacAndAutoMapperMVC.Infrastructure;
  using AutoMapper;
  
  public class AutoFacModule : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.Register(context =&gt; new MapperConfiguration(cfg =&gt;
          {
              //Register Mapper Profile
              cfg.AddProfile&lt;AutoMapperProfile&gt;();
          }
          )).AsSelf().InstancePerRequest();
  
          builder.Register(c =&gt;
          {
              //This resolves a new context that can be used later.
              var context = c.Resolve&lt;IComponentContext&gt;();
              var config = context.Resolve&lt;MapperConfiguration&gt;();
              return config.CreateMapper(context.Resolve);
          })
          .As&lt;IMapper&gt;()
          .InstancePerLifetimeScope();
      }
  }
  </code></pre>
  <p><code>AutoMapperProfile</code>:</p>
  <pre><code>using Roster.Entity.DatabaseEntities;
  using Roster.Entity.ViewModels;
  using AutoMapper;
  
  namespace AutoFacAndAutoMapperMVC.Infrastructure
  {
      public class AutoMapperProfile : Profile
      {
          public AutoMapperProfile()
          {
              CreateMap&lt;Shift, ShiftViewModel&gt;();
  
              CreateMap&lt;ShiftViewModel, Shift&gt;();
          }
      }
  }
  </code></pre>
  <p><a href="https://stackoverflow.com/users/4534800/trekco">Trekco</a>, It is called by a generic method of IEnumerable</p>
  <pre><code>public virtual IEnumerable&lt;T&gt; GetAll()
          {
              return _repository.GetAll();
          }
  </code></pre>
  <p>it returns a Shift object</p>