Automapper, Mapper Not initialized. Call initializ

2019-04-23 17:54发布

问题:

When I try to submit data into database i get following error:

{"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)

Please tell me why i am getting this error as i am not able to figure it out after so much effort.

Here is my controller code

public CompanyController() { }
private readonly ICompanyService _companyService;
public CompanyController(ICompanyService companyService) {
    _companyService = companyService;
}

This is the SaveData method in the controller

[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());
    }
}

My Form view code

@using (Html.BeginForm("SaveData", "Company", FormMethod.Post, new { @class = "", @id = "validate", @role = "form" })) { 
@*<form id="validate" method="POST" action="javascript:alert('Form #validate submited');">*@
<div class="head tac">
    <h1>Register Your Company With Us!!!</h1>
</div>
<div class="block">
    <div class="header">
        <div class="side pull-right">
            <button class="btn btn-default btn-clean" onclick="clear_form('#validate');" type="button">Clear form</button>
    </div>
</div>

<div class="content controls">
    <div class="form-row">
        <div class="col-md-3">Company Name:</div>
            <div class="col-md-9">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "validate[required,maxSize[100]]", @placeholder = @Html.DisplayNameFor(model => model.Name) } })

            </div>
        </div>
        <div class="form-row">
            <div class="col-md-3">Owner Name:</div>
                <div class="col-md-9">
                    @Html.EditorFor(model => model.Owner, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]", @placeholder = @Html.DisplayNameFor(model => model.Owner) } })

                </div>
            </div>
            <div class="form-row">
                <div class="col-md-3">Email:</div>
                    <div class="col-md-4">
                        @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]" ,@type="email", @placeholder = @Html.DisplayNameFor(model => model.Email) } })
                    </div>
                </div>
                <div class="form-row">
                    <div class="col-md-3">Password:</div>
                    <div class="col-md-9">
                        @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "validate[required,maxSize[100]]", @type="password", @id="password", @placeholder = @Html.DisplayNameFor(model => model.Password) } })


                    </div>
                </div>
                <div class="form-row">
                    <div class="col-md-3">Confirm Password:</div>
                    <div class="col-md-9">
                        @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "validate[required,equals[password]]", @type = "password", @placeholder = @Html.DisplayNameFor(model => model.Password) } })

                    </div>
                </div>
        @* I have omitted some code from here for saving your time to read*@  

                <div class="form-row">
                    <div class="col-md-3">Address:</div>
                    <div class="col-md-9">

                        @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "validate[required,maxSize[60]]", @placeholder = @Html.DisplayNameFor(model => model.Address) } })
                    </div>
                </div>
            </div>
            <div class="footer">
                <div class="side pull-right">
                    <div class="btn-group">
                        <button class="btn btn-default" type="button" onclick="$('#validate').validationEngine('hide');">Hide prompts</button>
                        <button class="btn btn-success" type="submit">Submit</button>
                    </div>
                </div>
            </div>
        </div>
   }

Here is my Company Model 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; }

This is ViewModel 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; }

Here is DomainToViewModelMapping Profile

cfg.CreateMap<Company, CompanyFormViewModel>()
    .ForMember(vm => vm.Id, map => map.MapFrom(m => m.Id))
    .ForMember(vm => vm.Name, map => map.MapFrom(m => m.Name))
    .ForMember(vm => vm.Owner, map => map.MapFrom(m => m.Owner))
    .ForMember(vm => vm.Email, map => map.MapFrom(m => m.Email))
    .ForMember(vm => vm.Password, map => map.MapFrom(m => m.Password))
    .ForMember(vm => vm.Address, map => map.MapFrom(m => m.Address));

My viewModeltoDomainMapping Profile

cfg.CreateMap<CompanyFormViewModel, Company>()
    .ForMember(m => m.Id, map => map.MapFrom(vm => vm.Id))
    .ForMember(m => m.Name, map => map.MapFrom(vm => vm.Name))
    .ForMember(m => m.Owner, map => map.MapFrom(vm => vm.Owner))
    .ForMember(m => m.Email, map => map.MapFrom(vm => vm.Email))
    .ForMember(m => m.Password, map => map.MapFrom(vm => vm.Password))
    .ForMember(m => m.Address, map => map.MapFrom(vm => vm.Address));

Why am I getting this error of mapper not initialized?

回答1:

Cannot see where you initialize the mapping.

Solution:

You can create your mapping profile, for example:

public class MappingProfile : Profile
{
    public MappingProfile()
    {
       CreateMap<Company, CompanyFormViewModel>()
          .ForMember(vm => vm.Id, map => map.MapFrom(m => m.Id))
          .ForMember(vm => vm.Name, map => map.MapFrom(m => m.Name))
          .ForMember(vm => vm.Owner, map => map.MapFrom(m => m.Owner))
          .ForMember(vm => vm.Email, map => map.MapFrom(m => m.Email))
          .ForMember(vm => vm.Password, map => map.MapFrom(m => m.Password))
          .ForMember(vm => vm.Address, map => map.MapFrom(m => m.Address));
       CreateMap<CompanyFormViewModel, Company>()
          .ForMember(m => m.Id, map => map.MapFrom(vm => vm.Id))
          .ForMember(m => m.Name, map => map.MapFrom(vm => vm.Name))
          .ForMember(m => m.Owner, map => map.MapFrom(vm => vm.Owner))
          .ForMember(m => m.Email, map => map.MapFrom(vm => vm.Email))
          .ForMember(m => m.Password, map => map.MapFrom(vm => vm.Password))
          .ForMember(m => m.Address, map => map.MapFrom(vm => vm.Address)); 

    }
}

And then initialize in mapping profile in Global.asax.cs

    public class AutoMapperConfiguration
    {
        public static void Configure()
        {
            Mapper.Initialize(x =>
            {
                x.AddProfile<MappingProfile>();
            });

            Mapper.Configuration.AssertConfigurationIsValid();               
        }
    }

Finally, don't forget to configure the auto mapper in your Application_Start()

//Configure Automapper
AutoMapperConfiguration.Configure();


回答2:

Instead of using static Mapper.Map method, you need to use mapper instance created via configuration, like following:

var mapper = cfg.CreateMapper();
var company = (Company)mapper.Map(companyFormViewModel, company, typeof(CompanyFormViewModel), typeof(Company));

Preferred way of working with mapper in the latest versions is via IMapper interface. If you are using DI container as well, you can bind/register IMapper to resolve instance via config.CreateMapper() method overload which takes instance factory delegate (i.e. container's instance factory method). If you are not using DI container, you could create for example MapperFactory class which will be responsible for the mapper instance creation and its configuration.

Example:

 var config = new MapperConfiguration(cfg =>
            {
                //cfg.CreateMap()...
                //cfg.AddProfile()... etc...
            });
var mapper = config.CreateMapper();