- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道有很多关于这个主题的问题,但没有一个能为我的问题提供答案。这就是我提出新问题的原因。
我在谷歌和此处寻找答案,并找到了一些改进了我的 AutoMapper 设置的答案。 (即只创建一次映射)
我的问题是我有时会得到异常,有时却不会。我完全不知道如何解决它。我已经搜索了一整天,但找不到任何东西。
当我运行 Mapper.AssertConfigurationIsValid() 时;它成功了。
我尝试添加以下内容:
Mapper.Configuration.AllowNullDestinationValues = true;
Mapper.AllowNullDestinationValues = true;
但我仍然得到错误 :s
我的设置如下:
在我的 global.asax 文件中,我调用了 AutoMapperSetup.SetupAutoMapper()。
protected void Application_Start(object sender, EventArgs e)
{
...
AutoMapperSetup.SetupAutoMapper();
}
AutoMapperSetup 类如下所述:(我知道我的静态构造函数正在执行。如果我放置断点,它就会被击中。所以代码确实会执行)
public static class AutoMapperSetup
{
static AutoMapperSetup()
{
#region GetPlanning
Mapper.CreateMap<GetPlanningResult_v3, FWSGetPlanningResult>()
.ForSourceMember(x => x.ItemSelection, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ExecutionResult, FWSExecutionResult>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ExecutionResult.Error, FWSExecutionResult.FWSError>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ExecutionResult.Warning, FWSExecutionResult.FWSWarning>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<GetPlanningHistoricalSelectionResult_v3, FWSGetPlanningHistoricalSelectionResult>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<PlanningResult_v3, FWSPlanningResult>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Anomaly, FWSAnomaly>()
.ForSourceMember(x => x.AnomalyID, y => y.Ignore())
.ForSourceMember(x => x.Dispatcher, y => y.Ignore())
.ForSourceMember(x => x.IsDealed, y => y.Ignore())
.ForSourceMember(x => x.AnomalyDateTime, y => y.Ignore())
.ForSourceMember(x => x.Trip, y => y.Ignore())
.ForSourceMember(x => x.Place, y => y.Ignore())
.ForSourceMember(x => x.Job, y => y.Ignore())
.ForSourceMember(x => x.Product, y => y.Ignore())
.ForSourceMember(x => x.Vehicle, y => y.Ignore())
.ForSourceMember(x => x.Driver, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<CommentHistoricalResult, FWSCommentHistoricalResult>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<IdentifierResult, FWSIdentifierResult>()
.ForSourceMember(x => x.Code, y => y.Ignore())
.ForSourceMember(x => x.ID, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<IdentifierPerson, FWSIdentifierPerson>()
.ForSourceMember(x => x.LastName, y => y.Ignore())
.ForSourceMember(x => x.FirstName, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<IdentifierVehicleResult, FWSIdentifierVehicleResult>()
.ForSourceMember(x => x.LicensePlate, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<TripResult_v3, FWSTripResult>()
.ForSourceMember(x => x.ModificationDate, y => y.Ignore())
.ForSourceMember(x => x.ModificationID, y => y.Ignore())
.ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
.ForSourceMember(x => x.EndDate, y => y.Ignore())
.ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
.ForSourceMember(x => x.CreatedByDriver, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Trip, FWSTrip>()
.ForSourceMember(x => x.References, y => y.Ignore())
.ForSourceMember(x => x.TypeCustom, y => y.Ignore())
.ForSourceMember(x => x.CustomNr, y => y.Ignore())
.ForSourceMember(x => x.StartTripAct, y => y.Ignore())
.ForSourceMember(x => x.StopTripAct, y => y.Ignore())
.ForSourceMember(x => x.OrderSeq, y => y.Ignore())
.ForSourceMember(x => x.Overwrite, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<PlaceResult_v2, FWSPlaceResult>()
.ForSourceMember(x => x.ModificationDate, y => y.Ignore())
.ForSourceMember(x => x.ModificationID, y => y.Ignore())
.ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
.ForSourceMember(x => x.SessionID, y => y.Ignore())
.ForSourceMember(x => x.Export, y => y.Ignore())
.ForSourceMember(x => x.ReadDateTime, y => y.Ignore())
.ForSourceMember(x => x.Format, y => y.Ignore())
.ForSourceMember(x => x.Documents, y => y.Ignore())
.ForSourceMember(x => x.TachoActivities, y => y.Ignore())
.ForSourceMember(x => x.EndDate, y => y.Ignore())
.ForSourceMember(x => x.DriverInfo, y => y.Ignore())
.ForSourceMember(x => x.AlarmDateTime, y => y.Ignore())
.ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
.ForSourceMember(x => x.CreatedByDriver, y => y.Ignore())
.ForSourceMember(x => x.Comments, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Place, FWSPlace>()
.ForSourceMember(x => x.References, y => y.Ignore())
.ForSourceMember(x => x.CustomNr, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Consultation, FWSConsultation>()
.ForSourceMember(x => x.ModificationDate, y => y.Ignore())
.ForSourceMember(x => x.ConsultationID, y => y.Ignore())
.ForSourceMember(x => x.Vehicle, y => y.Ignore())
.ForSourceMember(x => x.Driver, y => y.Ignore())
.ForSourceMember(x => x.Position, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ExtraInfo, FWSExtraInfo>()
.ForSourceMember(x => x.Date, y => y.Ignore())
.ForSourceMember(x => x.InfoID, y => y.Ignore())
.ForSourceMember(x => x.Export, y => y.Ignore())
.ForSourceMember(x => x.Activity, y => y.Ignore())
.ForSourceMember(x => x.Vehicle, y => y.Ignore())
.ForSourceMember(x => x.Driver, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Activity, FWSActivity>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<PlaceIdentifier, FWSPlaceIdentifier>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<TripIdentifier, FWSTripIdentifier>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Position, FWSPosition>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<PlanningItem, FWSPlanningItem>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<GeofencingResult, FWSGeofencingResult>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Geofencing, FWSGeofencing>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Comment, FWSComment>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<JobResult_v2, FWSJobResult>()
.ForSourceMember(x => x.Author, y => y.Ignore())
.ForSourceMember(x => x.Export, y => y.Ignore())
.ForSourceMember(x => x.CreatedbyDriver, y => y.Ignore())
.ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
.ForSourceMember(x => x.Transfer, y => y.Ignore())
.ForSourceMember(x => x.CancelStatus, y => y.Ignore())
.ForSourceMember(x => x.ModificationDate, y => y.Ignore())
.ForSourceMember(x => x.ModificationID, y => y.Ignore())
.ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ProductResult_v2, FWSProductResult>()
.ForSourceMember(x => x.ModificationDate, y => y.Ignore())
.ForSourceMember(x => x.ModificationID, y => y.Ignore())
.ForSourceMember(x => x.ModificationNumber, y => y.Ignore())
.ForSourceMember(x => x.Format, y => y.Ignore())
.ForSourceMember(x => x.ProductType, y => y.Ignore())
.ForSourceMember(x => x.Export, y => y.Ignore())
.ForSourceMember(x => x.Transfer, y => y.Ignore())
.ForSourceMember(x => x.ExternalSynchStatus, y => y.Ignore())
.ForSourceMember(x => x.CreatedByDriver, y => y.Ignore())
.ForSourceMember(x => x.TnrPlace, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Job, FWSJob>()
.ForSourceMember(x => x.OrderSeq, y => y.Ignore())
.ForSourceMember(x => x.DriverDisplay, y => y.Ignore())
.ForSourceMember(x => x.Comment, y => y.Ignore())
.ForSourceMember(x => x.CustomNr, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Product, FWSProduct>()
.ForSourceMember(x => x.References, y => y.Ignore())
.ForSourceMember(x => x.CustomNr, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ViaRouteItemResult, FWSViaRouteItemResult>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Customer, FWSCustomer>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Shipper, FWSShipper>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<ActivityPlace, FWSActivityPlace>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<Transfer, FWSTransfer>()
.ForSourceMember(x => x.SendDateTime, y => y.Ignore())
.ForSourceMember(x => x.TransferCancelID, y => y.Ignore())
.ForSourceMember(x => x.TransferDateTime, y => y.Ignore())
.ForSourceMember(x => x.TransferID, y => y.Ignore())
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<enumPlanningStatus, FWSEnumPlanningStatus>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<enumSynchronisationStatus, FWSEnumSynchronisationStatus>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<enumCancelStatus, FWSEnumCancelStatus>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<enumTransferStatus, FWSEnumTransferStatus>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<enumGeoFencingEventType, FWSenumGeoFencingEventType>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
Mapper.CreateMap<enumPositionFormat, FWSenumPositionFormat>()
.ForAllMembers(op => op.Condition(x => !x.IsSourceValueNull));
#endregion
#region InsertUpdatePlanning
Mapper.CreateMap<FWSPlanningInsert, PlanningInsert>();
Mapper.CreateMap<FWSPlanning, Planning>();
Mapper.CreateMap<FWSTripInsert, TripInsert>()
.ForMember(x => x.References, y => y.Ignore())
.ForMember(x => x.TypeCustom, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore())
.ForMember(x => x.StartTripAct, y => y.Ignore())
.ForMember(x => x.StopTripAct, y => y.Ignore())
.ForMember(x => x.OrderSeq, y => y.Ignore())
.ForMember(x => x.Overwrite, y => y.Ignore());
Mapper.CreateMap<FWSTrip, Trip>()
.ForMember(x => x.References, y => y.Ignore())
.ForMember(x => x.TypeCustom, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore())
.ForMember(x => x.StartTripAct, y => y.Ignore())
.ForMember(x => x.StopTripAct, y => y.Ignore())
.ForMember(x => x.OrderSeq, y => y.Ignore())
.ForMember(x => x.Overwrite, y => y.Ignore());
Mapper.CreateMap<FWSPlaceInsert, PlaceInsert>()
.ForMember(x => x.References, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore());
Mapper.CreateMap<FWSPlace, Place>()
.ForMember(x => x.References, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore());
Mapper.CreateMap<FWSJobInsert, JobInsert>()
.ForMember(x => x.OrderSeq, y => y.Ignore())
.ForMember(x => x.DriverDisplay, y => y.Ignore())
.ForMember(x => x.Comment, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore());
Mapper.CreateMap<FWSJob, Job>()
.ForMember(x => x.OrderSeq, y => y.Ignore())
.ForMember(x => x.DriverDisplay, y => y.Ignore())
.ForMember(x => x.Comment, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore());
Mapper.CreateMap<FWSProductInsert, ProductInsert>()
.ForMember(x => x.References, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore());
Mapper.CreateMap<FWSProduct, Product>()
.ForMember(x => x.References, y => y.Ignore())
.ForMember(x => x.CustomNr, y => y.Ignore());
Mapper.CreateMap<FWSIdentifier, Identifier>();
Mapper.CreateMap<FWSIdentifierVehicle, IdentifierVehicle>();
Mapper.CreateMap<FWSIdentifierSite, IdentifierSite>();
Mapper.CreateMap<FWSenumIdentifierType, enumIdentifierType>();
Mapper.CreateMap<FWSenumIdentifierVehicleType, enumIdentifierVehicleType>();
Mapper.CreateMap<FWSenumIdentifierSiteType, enumIdentifierSiteType>();
Mapper.CreateMap<FWSReference, Reference>();
Mapper.CreateMap<FWSActivity, Activity>();
Mapper.CreateMap<FWSGeofencing, Geofencing>();
Mapper.CreateMap<FWSPosition, Position>();
Mapper.CreateMap<FWSIdentifierViaRoute, IdentifierViaRoute>();
Mapper.CreateMap<FWSViaPointInsert, ViaPointInsert>();
Mapper.CreateMap<FWSViaPoint, BusinessFramework.Data.ViaRoute.ViaPoint>();
Mapper.CreateMap<FWSViaRouteInsert, ViaRouteInsert>();
Mapper.CreateMap<FWSViaRoute, ViaRoute>();
Mapper.CreateMap<FWSActivityPlace, ActivityPlace>();
Mapper.CreateMap<FWSenumGeoFencingEventType, enumGeoFencingEventType>();
Mapper.CreateMap<FWSenumPositionFormat, enumPositionFormat>();
Mapper.CreateMap<FWSReferenceProduct, ReferenceProduct>();
Mapper.CreateMap<FWSenumIdentifierViaRouteType, enumIdentifierViaRouteType>();
Mapper.CreateMap<FWSenumViaRouteType, enumViaRouteType>();
Mapper.CreateMap<FWSenumViaRoutePlanningType, enumViaRoutePlanningType>();
Mapper.CreateMap<FWSIdentifierAddress, IdentifierAddress>();
Mapper.CreateMap<FWSenumIdentifierAddressType, enumIdentifierAddressType>();
Mapper.CreateMap<FWSCustomer, Customer>();
Mapper.CreateMap<FWSShipper, Shipper>();
#endregion
#region InsertUpdateSettings
Mapper.CreateMap<FWSUpdateInsertSettings, UpdateInsertSettings>();
#endregion
#region Cancel Planning
Mapper.CreateMap<FWSPlanningItemSelection, PlanningItemSelection>();
#endregion
}
public static void SetupAutoMapper()
{
}
}
当我启动我的 Web 服务时,它会被调用。下一步是从数据库中获取数据并将其映射到我的 DTO:
FWSGetPlanningResult resultToReturn = Mapper.Map<GetPlanningResult_v3, FWSGetPlanningResult>(result);
执行上面的代码行给我映射异常错误。下面是异常的详细信息:
AutoMapper.AutoMapperMappingException occurred
HResult=-2146233088
Message=Missing type map configuration or unsupported mapping.
Mapping types:
GetPlanningResult_v3 -> FWSGetPlanningResult
BusinessFramework.Data.Planning.Result.GetPlanningResult_v3 -> BusinessFramework.Data.Planning.FWSGetPlanningResult
Destination path:
FWSGetPlanningResult
Source value:
BusinessFramework.Data.Planning.Result.GetPlanningResult_v3
Source=AutoMapper
StackTrace:
at Desktop.Web.Fleet.DBFactory.ConvertFromGetPlanningResult(GetPlanningResult_v3 result) in c:\TFSProjects\Tx-Framework\Framework\Main\Source\BusinessFramework\Services.Fleet\Classes\DB\Factories\Planning.cs:line 593
InnerException:
我希望有人能给我解决方案。正如您在对象数量上看到的那样。我并不急于开始手动映射所有内容。
提前致谢!
最佳答案
我的问题已经解决了。问题如下:
在我们的解决方案中,我们最近开始与 IOC 合作。有人用 AutoMapperTypeAdapter 创建了一个 TypeAdapter 作为引用实体。
当在我们的应用程序中打开或进入某个模块时,Automapper 再次被初始化,我在 global.asax 中创建的映射被删除。
将我的代码修改为也可以使用 TypeAdapter 后,我的问题就解决了。
感谢所有试图帮助我的人!
关于c# - AutoMapper.AutoMapperMappingException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238273/
当我收到此异常时,是否可以通过某种方式从automapper中获取更多详细信息: AutoMapper.AutoMapperMappingException 通常,它会告诉我这两种映射类型,但不会告诉
当我在我的代码中实现 IReadOnlyList 时,我的单元测试抛出了一个 AutoMapperMappingException。 通过深入研究文章和文档,我猜测 AutoMapper 需要对 re
我猜测除了异常之外的所有信息都是额外的并且几乎不相关(并且在这里是为了完整性)。问题是:根据下面的异常,到底遇到了什么问题? 我无法理解异常消息。据我所知,将 BsonDocument 映射到 Chi
public class ClientViewModel { [Required(ErrorMessage = "The Client Code field is requir
Automapper 出现特殊错误 错误信息: Mapping types: TransportOffer -> TransportOfferDto Model.TransportOffer -> D
_quatationRepository = new QuatationRepository(); IList quatationObj = _quatationRepository.GetAll(q
我有类似 this. 的问题但是这个答案对我不起作用。 我正在做一个小项目。我有两个域模型(Post,Source): public class Post { public Post()
在最近一两周内,我们的代码中开始出现一个奇怪的错误。我正在尝试确定映射失败的根本原因。最内部的异常本身就令人费解:Type 'System.String' does not have a defaul
我在 .NET CORE 应用程序中使用 AutoMapper。我在 ConfigureServices.cs 方法文件中的 Startup.cs 中配置了 AutoMapper,如下所示: publ
我在我的 ASP.NET Core 2.1 Web 应用程序中使用 AutoMapper 7.0.1 和 AutoMapper.Extensions.Microsoft.DependencyInjec
我的实体 public partial class Users { public Users() { sexs = new HashSet(); } [
我是一名优秀的程序员,十分优秀!