title: Automapper, Mapping config isn't persisting
body: <p>So the problem.</p>
  
  <p>Ive added</p>
  
  <pre><code>AutoMapperConfig.Configure();
  </code></pre>
  
  <p>to the application_Start in global.asax</p>
  
  <p>it runs the code</p>
  
  <pre><code>Mapper.Initialize(x =&gt;
  {
    x.AddProfile&lt;DomainToViewModelMappingProfile&gt;();
    x.AddProfile&lt;ViewModelToDomainMappingProfile&gt;();
  });
  
  Mapper.AssertConfigurationIsValid();
  </code></pre>
  
  <p>which runs</p>
  
  <pre><code>public class DomainToViewModelMappingProfile : Profile
  {
      protected override void Configure()
      {
  
          Mapper.CreateMap&lt;DBO.User, ViewModels.UserViewModel&gt;();
      }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class ViewModelToDomainMappingProfile : Profile
  {
      protected override void Configure()
      {
          Mapper.Initialize(cfg =&gt;
          {
              cfg.CreateMap&lt;ViewModels.UserViewModel, DBO.User&gt;();
          });
      }
  }
  </code></pre>
  
  <p>and everything compiles and runs fine.</p>
  
  <p>but in the controller:</p>
  
  <pre><code>UserViewModel model = new UserViewModel();
  User user = userService.GetUser(2);
  model = Mapper.Map&lt;User, UserViewModel&gt;(user); //this line fails as mapping doesnt exist
  return View();
  </code></pre>
  
  <p>but if i add the mapping config in the controller method</p>
  
  <pre><code>Mapper.CreateMap&lt;ViewModels.UserViewModel,DBO.User&gt;();
  UserViewModel model = new UserViewModel();
  User user = userService.GetUser(2);
  model = Mapper.Map&lt;User, UserViewModel&gt;(user); //Works great
  return View();
  </code></pre>
  
  <p>it works fine.</p>
  
  <p>ignore the different syntax with automapper. Ive tried the deprecated and new way of mapping and both fail.</p>
  
  <p>Thanks</p>