Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper, Mapper Not initialized. Call initialize with proper configuration

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?

like image 219
mac Avatar asked May 18 '17 06:05

mac


People also ask

Where is AutoMapper configuration?

Where do I configure AutoMapper? ¶ Configuration should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.

What can I use instead of AutoMapper?

AutoMapper is one of the popular object-object mapping libraries with over 296 million NuGet package downloads. It was first published in 2011 and its usage is growing ever since. Mapster is an emerging alternative to AutoMapper which was first published in 2015 and has over 7.4 million NuGet package downloads.

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

What is AutoMapper in C# with example?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.


1 Answers

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();
like image 197
Darjan Bogdan Avatar answered Sep 22 '22 04:09

Darjan Bogdan