自动映射器内部异常IEnumerable <office>到List <listofficemodel>

时间:2017-05-26 00:43:46

标签: c# automapper

我的AutoMapper在var结果行上抛出异常。我不知道我错过了什么,这个例外的原因是什么

堆栈输出

  

AutoMapper.AutoMapperMappingException:错误映射类型。

     

映射类型:IEnumerable 1 -> List 1   System.Collections.Generic.IEnumerable 1[[RoomBooking.Domain.Office, RoomBooking.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List 1 [[RoomBooking.Application.Couties.Queries.ListOfficeModel,   RoomBooking.Application,Version = 1.0.0.0,Culture = neutral,   PublicKeyToken = null]] ---&gt; AutoMapper.AutoMapperMappingException:   缺少类型映射配置或不支持的映射。

     

映射类型:Office - &gt; ListOfficeModel RoomBooking.Domain.Office - &gt;   RoomBooking.Application.Couties.Queries.ListOfficeModel at   lambda_method(Closure,Office,ListOfficeModel,ResolutionContext)   在AutoMapper.ResolutionContext.Map [TSource,TDestination](TSource   来源,TDestination目的地)在lambda_method(Closure,   IEnumerable 1 , List 1,ResolutionContext)---内在的结束   异常堆栈跟踪---在lambda_method(Closure,IEnumerable 1 , List 1,ResolutionContext)at at   AutoMapper.Mapper.AutoMapper.IMapper.Map [TSource,TDestination](TSource   来源)   RoomBooking.Application.Couties.Queries.GetOfficesListQuery.Execute()   在   d:\ TFS \书,一室的\ src \ RoomBooking \ RoomBooking.Application \办公室\查询\ GetOfficesListQuery.cs:行   21在RoomBooking.Presentation.Controllers.OfficeController.Get()   在   d:\ TFS \书,一室的\ src \ RoomBooking \ RoomBooking.Presentation \ \控制器OfficeController.cs:行   18在lambda_method(Closure,Object,Object [])at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__27.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__25.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext   上下文)   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(州及放大器;   接下来,范围&amp;范围,对象&amp; state,Boolean&amp; isCompleted)at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext   上下文)   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(州及放大器;   接下来,范围&amp;范围,对象&amp; state,Boolean&amp; isCompleted)at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Microsoft.AspNetCore.Session.SessionMiddleware.d__9.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   Microsoft.AspNetCore.Session.SessionMiddleware.d__9.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Microsoft.VisualStudio.Web.BrowserLink.BrowserLinkMiddleware.d__7.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

public class ListOfficeModel
{
    public long RecordId { get; set; }

    public long OfficeId { get; set; }
    public string DisplayName { get; set; }
    public int? DiplayOrder { get; set; }
    public long? CountyId { get; set; }
    public bool? Deleted { get; set; }
    public bool? IsBranch { get; set; }
}
    public class AgregateRoot
{

    public Guid UniqueId { get; set; }
    public bool IsDeleted { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime ModifiedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
}
public class Office : AgregateRoot
{
    [Key]
    public long RecordId { get; set; }

    public long OfficeId { get; set; }
    public string  DisplayName { get; set; }
    public int? DiplayOrder { get; set; }
    public long? CountyId { get; set; }
    public bool? Deleted { get; set; }
    public bool? IsBranch { get; set; }
}

        public List<ListOfficeModel> Execute()
        {
            var offices = this.databaseService.SelectOfficesforList();

            Mapper.Initialize(cfg => {
                cfg.CreateMap<Office, ListOfficeModel>();
            });
//the exception happen here 
            var result = Mapper.Map<IEnumerable<Office>, List<ListOfficeModel>>(offices);
            return result;
        }

1 个答案:

答案 0 :(得分:1)

您的代码示例在干净安装的AutoMapper 6.0.2上运行良好。

一些突出的错误是你在Execute方法中初始化你的映射器。您应该在应用程序启动时初始化Mapper 一次并且每个应用程序只初始化一次。移动Mapper.Initialize部分,看看它是否有效。