2017-10-29 179 views
0

我一直在阅读有关此问题的类似问题,但似乎无法理解解决方案。映射注册时丢失类型映射配置或不支持的映射

我想映射我的实体到我的视图模型,我注意到它失败了一些我的实体。一个例子是以下几点:

实体

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; } 
} 

视图模型

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; } 
} 

映射

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

用法

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

异常

Exception

[AutoMapperMappingException:缺少类型映射配置或 不受支持的映射。

映射类型:WarehouseVM - >仓库WMAS.Shared.Models.WarehouseVM - > WMAS.Data.Entities.Warehouse] lambda_method(封闭,对象,对象,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
2013 \ Projects \ JET.P中的WMAS.Web.Areas.Warehouse.Controllers.WarehouseController.Create(WarehouseCreateVM viewModel)位于c:\ Users \ tugadoje \ Documents \ Visual Studio中 roject \ 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,IDictionary的参数)93
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() 90 System.Web.Mvc .Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult,ActionInvocation innerInvokeState)+45
S ystem.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +211
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 对象标签)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.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 对象标记)+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.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 对象标签)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.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +188
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 对象标签)73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult的asyncResult, 对象标签)50
的System.Web .Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult)68 System.Web.Mvc.Controller.b__15(IAsyncResult的 asyncResult,控制器控制器)60
System.Web.Mvc.Async.WrappedAsyncVoid 1.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.AsyncResu ltWrapper.End(IAsyncResult的asyncResult, 对象标签)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.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +188
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()196
系统.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag)+73
System.Web.Mvc.Async.AsyncResultWrapper.End(IA syncResult asyncResult, 对象标签)50
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult) 58 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的 结果)59
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 606 System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔& completedSynchronously)+ 288

我已经取得了一定注册我的所有映射。在我的global.acx.cs中,我打电话给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(); 
    } 
} 

任何帮助表示赞赏。 TIA。

回答

2

的关键是向开头的消息的这一部分:

缺少类型映射配置或不支持的映射。

映射类型:WarehouseVM - >仓库

您需要创建映射,以支持这一点:

cfg.CreateMap<WarehouseVM , Warehouse>(); 
+0

将此标记为与首先回答的正确答案。 –

2

的异常告诉你什么是错的:

缺少类型地图配置或不支持的映射。

和丢失的映射是:

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

检查您MapperConfig我没有看到来自WarehousVM映射到Warehouse。添加映射应该可以解决问题。

+0

阅读答案后,我觉得很蠢。一定是过于强调不能注意到一个。我不确定首先回答哪个问题,您或者@AaronLS,但是假期结束后4天内我会回到这个问题并进行测试。 –

相关问题