0
votes

I've been reading similar questions regarding this problem but I can't seem to understand the solution.

I'm trying to map my entity to my view model and I noticed that it fails for some of my entities. An example would be the following:

Entity

public class Warehouse : EntityBase
{
    [Index(IsUnique = true)]
    [MaxLength(20)]
    [Required]
    public string Code { get; set; }
    [Required]
    public string Name { get; set; }
    public string Description { get; set; }
    public int AddressId { get; set; }

    [ForeignKey("AddressId")]
    public virtual Address Address { get; set; }
    public virtual ICollection<Location> Locations { get; set; }

    public Warehouse()
    {

    }
}

public abstract class EntityBase : IConcurrencyEnabled
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Timestamp]
    public byte[] RowVersion { get; set; }
    public string CreatedBy { get; set; }
    public DateTime DateCreated { get; set; }
}  

View Model

public class WarehouseVM : VMBase
{
    public string Code { get; set; }
    [Required]
    public string Name { get; set; }
    public string Description { get; set; }
    public int AddressId { get; set; }

    public virtual AddressVM Address { get; set; }
    public virtual ICollection<LocationVM> Locations { get; set; }

    public WarehouseVM()
    {

    }
}  

public abstract class VMBase
{
    public int Id { get; set; }
    public byte[] RowVersion { get; set; }
    public string CreatedBy { get; set; }
    public DateTime DateCreated { get; set; }
}

Mapping

public static TDestination Map<TSource, TDestination>(this TSource entity)
    where TSource : class
    where TDestination : class, new()
{
    IMapper mapper = Core.Container.Get<IMapper>();

    return mapper.Map<TDestination>(entity);
}

Usage

WMAS.Data.Entities.Warehouse warehouse = viewModel.Item.Map<WarehouseVM, WMAS.Data.Entities.Warehouse>();

Exception

Exception

[AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: WarehouseVM -> Warehouse WMAS.Shared.Models.WarehouseVM -> WMAS.Data.Entities.Warehouse] lambda_method(Closure , Object , Object , ResolutionContext ) +468
AutoMapper.Mapper.AutoMapper.IMapper.Map(Object source) +603
WMAS.Web.Extension.Map(TSource entity) in c:\Users\tugadoje\Documents\Visual Studio 2013\Projects\JET.Project\WMAS.Web\Extensions\Extension.cs:40
WMAS.Web.Areas.Warehouse.Controllers.WarehouseController.Create(WarehouseCreateVM viewModel) in c:\Users\tugadoje\Documents\Visual Studio 2013\Projects\JET.Project\WMAS.Web\Areas\Warehouse\Controllers\WarehouseController.cs:73 lambda_method(Closure , ControllerBase , Object[] ) +192
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +81
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +752
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +93
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +90 System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +45
System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +211
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +141
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +65
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3c() +189 System.Web.Mvc.Async.<>c__DisplayClass45.b__3e() +717 System.Web.Mvc.Async.<>c__DisplayClass30.b__2f(IAsyncResult asyncResult) +66
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +141
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +65
System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +103 System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +330
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +58
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +90
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +188
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +68 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +60
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +85
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +58
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +93
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +188
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +58 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +59
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

I've made sure to register all my mappings. In my global.acx.cs I am calling MapperConfig.RegisterMappings().

public class MapperConfig
{
    public static MapperConfiguration MapperCfg { get; private set; }
    public static IMapper Mapper { get; private set; }

    public static void RegisterMappings()
    {
        MapperCfg = new MapperConfiguration(cfg =>
        {
            cfg.AllowNullCollections = true;
            cfg.AllowNullDestinationValues = true;
            cfg.CreateMap<Address, AddressVM>().ReverseMap();
            cfg.CreateMap<ApplicationUserDepartment, ApplicationUserDepartmentVM>().ReverseMap();
            cfg.CreateMap<BasicInfo, BasicInfoVM>().ReverseMap();
            cfg.CreateMap<CityMunicipality, CityMunicipalityVM>().ReverseMap();
            cfg.CreateMap<Company, CompanyVM>().ReverseMap();
            cfg.CreateMap<Country, CountryVM>().ReverseMap();
            cfg.CreateMap<Customer, CustomerVM>().ReverseMap();
            cfg.CreateMap<DeliverySlip, DeliverySlipVM>().ReverseMap();
            cfg.CreateMap<DeliverySlipItem, DeliverySlipItemVM>().ReverseMap();
            cfg.CreateMap<Department, DepartmentVM>().ReverseMap();
            cfg.CreateMap<Image, ImageVM>().ReverseMap();
            cfg.CreateMap<InventBatch, InventBatchVM>().ReverseMap();
            cfg.CreateMap<InventLedger, InventLedgerVM>().ReverseMap();
            cfg.CreateMap<Item, ItemVM>().ReverseMap();
            cfg.CreateMap<ItemCategory, ItemCategoryVM>().ReverseMap();
            cfg.CreateMap<Location, LocationVM>().ReverseMap();
            cfg.CreateMap<Module, ModuleVM>().ReverseMap();
            cfg.CreateMap<Province, ProvinceVM>().ReverseMap();
            cfg.CreateMap<PurchaseOrder, PurchaseOrderVM>().ReverseMap();
            cfg.CreateMap<PurchaseOrderItem, PurchaseOrderItemVM>().ReverseMap();
            cfg.CreateMap<Region, RegionVM>().ReverseMap();
            cfg.CreateMap<SalesInvoice, SalesInvoiceVM>().ReverseMap();
            cfg.CreateMap<SalesInvoiceItem, SalesInvoiceItemVM>().ReverseMap();
            cfg.CreateMap<SalesInvoicePayment, SalesInvoicePaymentVM>().ReverseMap();
            cfg.CreateMap<Submodule, SubmoduleVM>().ReverseMap();
            cfg.CreateMap<SysConfig, SysConfigVM>().ReverseMap();
        });

        Mapper = MapperCfg.CreateMapper();
    }
}

Any help is appreciated. TIA.

2

2 Answers

2
votes

The key is this part of the message towards the beginning:

Missing type map configuration or unsupported mapping.

Mapping types: WarehouseVM -> Warehouse

You need to create the mapping to support this:

cfg.CreateMap<WarehouseVM , Warehouse>();
2
votes

The exception tells you what is wrong:

Missing type map configuration or unsupported mapping.

and the missing mapping is:

Warehouse WMAS.Shared.Models.WarehouseVM -> WMAS.Data.Entities.Warehouse

Inspecting your MapperConfig I see no mapping from WarehousVM to Warehouse. Adding the mapping should fix the problem.