Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper Update from 3.3.1 to 4.0.4 fails mapping

Tags:

c#

automapper

The following code functions perfectly in AutoMapper 3.3.1.

public ProductVM GetProductDetailVM(int id)
{
    try
    {
        Mapper.CreateMap<Product, ProductVM>();
        var model = this._productRep.SingleOrDefault(d => d.ProductId== id);
        ProductVM vm = Mapper.Map<Product, ProductVM>(model);
        vm.Status = this._statusRep.FirstOrDefault(d => d.StatusID == model.StatusID);

        vm.FullName = vm.ForeName + " "
                      + (string.IsNullOrEmpty(vm.MiddleName.Trim()) ? string.Empty : vm.MiddleName + " ")
                      + vm.SurName;
        return vm;
    }
    catch (Exception)
    {
        return null;
    }
}

However, I have decided to get the latest version of AutoMapper (4.0.4) And I am getting the following error saying that

"Missing type map configuration or unsupported mapping.\n\nMapping "

What would be causing the issue?

like image 714
akd Avatar asked Oct 24 '25 15:10

akd


1 Answers

I also ran into the same problem, which was driving me crazy. I just realised it is something with the update from v3.3.1 to v4.0.4. The following UnitTest illustrates the error. I don't know which change they introduced but it breaks if there is a constructor with the IEnumerable < SelectListItem > testList parameter.

public class TestEntity
{
    public int Id { get; set; }
}

public class TestEntityViewModel
{
    public TestEntityViewModel()
    {

    }

    public TestEntityViewModel(IEnumerable<SelectListItem> testList)
    {
        TestList = testList;
    }

    public int Id { get; set; }
    public virtual IEnumerable<SelectListItem> TestList { get; set; }
}


[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        Mapper.CreateMap<TestEntity, TestEntityViewModel>()
            .ForMember(m => m.TestList, opt => opt.Ignore());

        var entity = new TestEntity();

        var viewModel = Mapper.Map<TestEntityViewModel>(entity);


        Assert.IsNotNull(viewModel);
    }
}
like image 82
Arturo Ribes Avatar answered Oct 27 '25 06:10

Arturo Ribes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!