C# 自动映射器内部异常IEnumerable<;办公室>;列出<;ListOfficeModel>;
我的AutoMapper在var结果行上抛出一个异常。我不知道我遗漏了什么,这种异常的原因是什么 堆栈输出 AutoMapper.AutoMappingException:错误映射类型 映射类型:IEnumerableC# 自动映射器内部异常IEnumerable<;办公室>;列出<;ListOfficeModel>;,c#,automapper,C#,Automapper,我的AutoMapper在var结果行上抛出一个异常。我不知道我遗漏了什么,这种异常的原因是什么 堆栈输出 AutoMapper.AutoMappingException:错误映射类型 映射类型:IEnumerable1->List1 System.Collections.Generic.IEnumerable1[[RoomBooking.Domain.Office, RoomBooking.Domain,版本=1.0.0.0,文化=中性, PublicKeyToken=null]]-> Sys
1->List
1
System.Collections.Generic.IEnumerable1[[RoomBooking.Domain.Office,
RoomBooking.Domain,版本=1.0.0.0,文化=中性,
PublicKeyToken=null]]->
System.Collections.Generic.List
1[[RoomBooking.Application.Couties.Queries.ListOfficeModel,
RoomBooking.Application,版本=1.0.0.0,文化=中性,
PublicKeyToken=null]]-->AutoMapper.AutoMappingException:
缺少类型映射配置或不支持的映射
映射类型:Office->ListOfficeModelRoomBooking.Domain.Office->
RoomBooking.Application.Couties.Queries.ListOfficeModel位于
lambda_方法(关闭、办公室、ListOfficeModel、ResolutionContext)
在AutoMapper.ResolutionContext.Map[TSource,TDestination](TSource
lambda_方法(闭合)下的源、t目的地),
IEnumerable1,List
1,ResolutionContext)--内部
异常堆栈跟踪---在lambda_方法处(闭包,IEnumerable1
,列表
1,ResolutionContext)位于
AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource
来源)在
RoomBooking.Application.Couties.Queries.GetOfficesListQuery.Execute()
在里面
D:\TFS\Book-a-Room\src\RoomBooking\RoomBooking.Application\Offices\querys\GetOfficesListQuery.cs:line
21在RoomBooking.Presentation.Controller.OfficeController.Get()中
在里面
D:\TFS\Book-a-Room\src\RoomBooking\RoomBooking.Presentation\Controllers\OfficeController.cs:line
18 at lambda_方法(闭包、对象、对象[])at
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u27.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u25.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext
(上下文)在
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态&
接下来,在
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u22.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext
(上下文)在
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(状态&
接下来,在
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u20.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Builder.RouterMiddleware.d_u4.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Session.SessionMiddleware.d_u9.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
Microsoft.AspNetCore.Session.SessionMiddleware.d_u9.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.VisualStudio.Web.BrowserLink.BrowserLinkMiddleware.d_u7.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d_u7.MoveNext()
公共类ListOfficeModel
{
公共长记录ID{get;set;}
公共长OfficeId{get;set;}
公共字符串DisplayName{get;set;}
公共int?DiplayOrder{get;set;}
}
公课办公室
{
[关键]
公共长记录ID{get;set;}
公共长OfficeId{get;set;}
公共字符串DisplayName{get;set;}
公共int?DiplayOrder{get;set;}
公共long?CountyId{get;set;}
公共bool?已删除{get;set;}
公共bool?IsBranch{get;set;}
}
公共列表执行()
{
var offices=this.databaseService.SelectOfficesforList();
Mapper.Initialize(cfg=>{
CreateMap();
});
//例外情况发生在这里
var result=Mapper.Map(办公室);
返回结果;
}
您的代码示例在干净安装的AutoMapper 6.0.2上运行良好
有一点是不正确的,那就是在Execute方法中初始化映射器。您应该在应用程序启动时为每个应用程序初始化映射器一次,并且仅初始化一次。移动映射器。在那里初始化部分,看看它是否工作。I include Aggre
public class ListOfficeModel
{
public long RecordId { get; set; }
public long OfficeId { get; set; }
public string DisplayName { get; set; }
public int? DiplayOrder { get; set; }
}
public class Office
{
[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;
}