title: Automapper, Mapper Not initialized. Call initialize with proper configuration
body: <p>When I try to submit data into database i get following error:</p>
  
  <blockquote>
    <p>{"Success":false,"Error":true,"ErrorType":2,"Message":"System.InvalidOperationException: Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you\u0027re using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.\r\n   at AutoMapper.Mapper.get_Instance()\r\n   at AutoMapper.Mapper.Map(Object source, Object destination, Type sourceType, Type destinationType)\r\n   at GoalPear.Web.Areas.PM.Controllers.CompanyController.SaveData(CompanyFormViewModel companyFormViewModel)</p>
  </blockquote>
  
  <p>Please tell me why i am getting this error as i am not able to figure it out after so much effort.</p>
  
  <p>Here is my controller code</p>
  
  <pre><code>public CompanyController() { }
  private readonly ICompanyService _companyService;
  public CompanyController(ICompanyService companyService) {
      _companyService = companyService;
  }
  </code></pre>
  
  <p>This is the SaveData method in the controller</p>
  
  <pre><code>[HttpPost]
  public JsonResult SaveData(CompanyFormViewModel companyFormViewModel)
  {
      try
      {
          int? id = null;
          Company company;
          if (companyFormViewModel.Id == null)
          {
              company = new Company();
          }
          else
          {
              id = (int)companyFormViewModel.Id;
              company = _companyService.GetCompany((int)id);
          }
  
          company = (Company)Mapper.Map(
              companyFormViewModel, 
              company, 
              typeof(CompanyFormViewModel), 
              typeof(Company));
          CompanyValidator companyValidator = new CompanyValidator();
          ValidationResult validationResult = companyValidator.Validate(company);
  
          if (validationResult.IsValid) //check for any validation errors
          {
              if (id == null)
              {
                  _companyService.CreateCompany(company);
              }
              else
              {
                  _companyService.Update(company);
              }
  
              _companyService.SaveCompany();
              return new JsonSuccessResult();
          }
          else
          {
              Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
              return new JsonErrorResult(validationResult);
          }
      }
      catch (Exception ex)
      {
          Response.StatusCode = (int)ResponseCode.UnprocessableEntity;
          return new JsonErrorResult(ex.ToString());
      }
  }
  </code></pre>
  
  <p>My Form view code</p>
  
  <pre><code>@using (Html.BeginForm("SaveData", "Company", FormMethod.Post, new { @class = "", @id = "validate", @role = "form" })) { 
  @*&lt;form id="validate" method="POST" action="javascript:alert('Form #validate submited');"&gt;*@
  &lt;div class="head tac"&gt;
      &lt;h1&gt;Register Your Company With Us!!!&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="block"&gt;
      &lt;div class="header"&gt;
          &lt;div class="side pull-right"&gt;
              &lt;button class="btn btn-default btn-clean" onclick="clear_form('#validate');" type="button"&gt;Clear form&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="content controls"&gt;
      &lt;div class="form-row"&gt;
          &lt;div class="col-md-3"&gt;Company Name:&lt;/div&gt;
              &lt;div class="col-md-9"&gt;
                  @Html.EditorFor(model =&gt; model.Name, new { htmlAttributes = new { @class = "validate[required,maxSize[100]]", @placeholder = @Html.DisplayNameFor(model =&gt; model.Name) } })
  
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-row"&gt;
              &lt;div class="col-md-3"&gt;Owner Name:&lt;/div&gt;
                  &lt;div class="col-md-9"&gt;
                      @Html.EditorFor(model =&gt; model.Owner, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]", @placeholder = @Html.DisplayNameFor(model =&gt; model.Owner) } })
  
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-row"&gt;
                  &lt;div class="col-md-3"&gt;Email:&lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          @Html.EditorFor(model =&gt; model.Email, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]" ,@type="email", @placeholder = @Html.DisplayNameFor(model =&gt; model.Email) } })
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-row"&gt;
                      &lt;div class="col-md-3"&gt;Password:&lt;/div&gt;
                      &lt;div class="col-md-9"&gt;
                          @Html.EditorFor(model =&gt; model.Password, new { htmlAttributes = new { @class = "validate[required,maxSize[100]]", @type="password", @id="password", @placeholder = @Html.DisplayNameFor(model =&gt; model.Password) } })
  
  
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-row"&gt;
                      &lt;div class="col-md-3"&gt;Confirm Password:&lt;/div&gt;
                      &lt;div class="col-md-9"&gt;
                          @Html.EditorFor(model =&gt; model.Password, new { htmlAttributes = new { @class = "validate[required,equals[password]]", @type = "password", @placeholder = @Html.DisplayNameFor(model =&gt; model.Password) } })
  
                      &lt;/div&gt;
                  &lt;/div&gt;
          @* I have omitted some code from here for saving your time to read*@  
  
                  &lt;div class="form-row"&gt;
                      &lt;div class="col-md-3"&gt;Address:&lt;/div&gt;
                      &lt;div class="col-md-9"&gt;
  
                          @Html.EditorFor(model =&gt; model.Address, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]", @placeholder = @Html.DisplayNameFor(model =&gt; model.Address) } })
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="footer"&gt;
                  &lt;div class="side pull-right"&gt;
                      &lt;div class="btn-group"&gt;
                          &lt;button class="btn btn-default" type="button" onclick="$('#validate').validationEngine('hide');"&gt;Hide prompts&lt;/button&gt;
                          &lt;button class="btn btn-success" type="submit"&gt;Submit&lt;/button&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
     }
  </code></pre>
  
  <p>Here is my Company Model code</p>
  
  <pre><code>public string Name {get;set;}
  public string Logo { get; set; }
  public string Address { get; set; }
  public string Owner { get; set; }
  public int Size { get; set; }
  public string Email { get; set; }
  public int Phone { get; set; }
  public int FaxNo { get; set; }
  public string Password { get; set; }
  public string Country { get; set; }
  public string State { get; set; }
  </code></pre>
  
  <p>This is ViewModel Code</p>
  
  <pre><code>public CompanyFormViewModel() { }
  public int? Id { get; set; }
  [DisplayName(" Company Name")]
  public string Name { get; set; }
  [DisplayName("Owner")]
  public string Owner { get; set; }
  [DisplayName("Email")]
  public string Email { get; set; }
  [DisplayName("Password")]
  public string Password { get; set; }
  [DisplayName("Address")]
  public string Address { get; set; }
  </code></pre>
  
  <p>Here is DomainToViewModelMapping Profile</p>
  
  <pre><code>cfg.CreateMap&lt;Company, CompanyFormViewModel&gt;()
      .ForMember(vm =&gt; vm.Id, map =&gt; map.MapFrom(m =&gt; m.Id))
      .ForMember(vm =&gt; vm.Name, map =&gt; map.MapFrom(m =&gt; m.Name))
      .ForMember(vm =&gt; vm.Owner, map =&gt; map.MapFrom(m =&gt; m.Owner))
      .ForMember(vm =&gt; vm.Email, map =&gt; map.MapFrom(m =&gt; m.Email))
      .ForMember(vm =&gt; vm.Password, map =&gt; map.MapFrom(m =&gt; m.Password))
      .ForMember(vm =&gt; vm.Address, map =&gt; map.MapFrom(m =&gt; m.Address));
  </code></pre>
  
  <p>My viewModeltoDomainMapping Profile</p>
  
  <pre><code>cfg.CreateMap&lt;CompanyFormViewModel, Company&gt;()
      .ForMember(m =&gt; m.Id, map =&gt; map.MapFrom(vm =&gt; vm.Id))
      .ForMember(m =&gt; m.Name, map =&gt; map.MapFrom(vm =&gt; vm.Name))
      .ForMember(m =&gt; m.Owner, map =&gt; map.MapFrom(vm =&gt; vm.Owner))
      .ForMember(m =&gt; m.Email, map =&gt; map.MapFrom(vm =&gt; vm.Email))
      .ForMember(m =&gt; m.Password, map =&gt; map.MapFrom(vm =&gt; vm.Password))
      .ForMember(m =&gt; m.Address, map =&gt; map.MapFrom(vm =&gt; vm.Address));
  </code></pre>
  
  <p>Why am I getting this error of mapper not initialized?</p>