我一直在閱讀有關此問題的類似問題,但似乎無法理解解決方案。映射註冊時丟失類型映射配置或不支持的映射
我想映射我的實體到我的視圖模型,我注意到它失敗了一些我的實體。一個例子是以下幾點:
實體
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>();
異常
[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.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +211
1.End()196
System.Web.Mvc.Async.WrappedAsyncResultBase
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.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +71
1.End()196
System.Web.Mvc.Async.WrappedAsyncResultBase
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.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +71
1.End()196
System.Web.Mvc.Async.WrappedAsyncResultBase
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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +188
1.End()+196
System.Web.Mvc.Async.WrappedAsyncResultBase
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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +85
1.End()+ 196
System.Web.Mvc.Async.WrappedAsyncResultBase
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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +188
1.End()196
System.Web.Mvc.Async.WrappedAsyncResultBase
系統.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。
將此標記爲與首先回答的正確答案。 –