AutoMapper中的嵌套列表到数组映射

时间:2015-11-28 09:46:44

标签: list nested mapping automapper

Domain Emtity中的以下类:

    public class SecondaryCustomerProfile
    {
      public int Id { get; set; }
      public List<SecondaryCustomerProfileProductType> ProductTypes { get; set; }
    }

    public class SecondaryCustomerProfileProductType
    {
        public int IdField { get; set; }
        public int SecondaryCustomerProfileIdField { get; set; }
        public string TypeField { get; set; }
    }

    //The following class in WCF Service

    public class SecondaryCustomerProfile 
    {
       public int Id { get; set; }
       public SecondaryCustomerProfileProductType[] ProductTypes { get; set; }
    }

    public class SecondaryCustomerProfileProductType
    {
        public int Id{ get; set; }
        public int SecondaryCustomerProfileIdField { get; set; }
        public string Type{ get; set; }
    }

现在,当我尝试映射如下...

 Mapper.CreateMap<Core.DomainEntities.SecondaryCustomerProfile, Core.CustomerService.SecondaryCustomerProfile>()
    .ForMember(d => d.Id, m => m.MapFrom(s => s.Id))
    .ForMember(d => d.ProductTypes, m => m.MapFrom(s => s.ProductTypes));

Mapper.CreateMap<Core.DomainEntities.SecondaryCustomerProfileProductType, Core.CustomerService.SecondaryCustomerProfileProductType>()
    .ForMember(d => d.Id, m => m.MapFrom(s => s.IdField))
    .ForMember(d => d.SecondaryCustomerProfileId, m => m.MapFrom(s => s.SecondaryCustomerProfileIdField))
    .ForMember(d => d.Type, m => m.MapFrom(s => s.TypeField));

Mapper.Map<DomainEntities.SecondaryCustomerProfile, CustomerService.SecondaryCustomerProfile>(profile);

我收到错误:

AutoMapper.AutoMapperMappingException was unhandled by user code
HResult=-2146233088
Message=Missing type map configuration or unsupported mapping.

Mapping types:
SecondaryCustomerProfile -> SecondaryCustomerProfile
DomainEntities.SecondaryCustomerProfile -> CustomerService.SecondaryCustomerProfile

Destination path:
SecondaryCustomerProfile

Source value:
DomainEntities.SecondaryCustomerProfile
  Source=AutoMapper
  StackTrace:
       at Services\Customer\CustomerCoreService.cs:line 75
       at CustomerController.Save(List`1 secondaryCustomerProfileModels) in C:\Test\CustomerController.cs:line 99
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
  InnerException: 
.

0 个答案:

没有答案