AutoMapper.AutoMapperMappingException [英] AutoMapper.AutoMapperMappingException

查看:592
本文介绍了AutoMapper.AutoMapperMappingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多问题都已经问关于这个话题,但没有人对我的问题提供了答案。这就是为什么我创建一个新的问题。

I know a lot of questions have been asked regarding this topic, but none of them provided an answer for my problem. That's why I'm creating a new question.

我看着谷歌和这里的答案,并发现了一些已提高了我的AutoMapper的设置。 (即仅创建映射仅一次)

I've looked on google and here for answers and have found some which have improved my setup of AutoMapper. (i.e. only creating your mappings only once)

我的问题是,我有时会收到异常,有时我不知道。我完全不知道如何解决它。我一直在寻找,现在一整天,我无法找到任何东西。

My problem is that I sometimes get the exception and sometimes I don't. And I have absolutely no idea how to solve it. I've been searching a whole day now and I can't find anything.

当我运行Mapper.AssertConfigurationIsValid();它成功

When I run Mapper.AssertConfigurationIsValid();it succeeds.

我试着添加以下内容:

Mapper.Configuration.AllowNullDestinationValues = true;
Mapper.AllowNullDestinationValues = true;  

和我仍然得到错误:■

我的设置是如下:

在我的Global.asax文件我叫AutoMapperSetup.SetupAutoMapper()

In my global.asax file I call AutoMapperSetup.SetupAutoMapper().

protected void Application_Start(object sender, EventArgs e)
    {
        ...
        AutoMapperSetup.SetupAutoMapper();
    }



AutoMapperSetup类描述如下:(我知道,我的静态构造函数是得到执行。如果我把一个断点,它击中。这样的代码不会得到执行)

The AutoMapperSetup class is described below: (I do know that my static constructor is getting executed. If I put a breakpoint, it's getting hit. So that code does get executed)

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:

This gets called when I start my WebServices. The next step is to get the data from DB and map this to my DTO:

FWSGetPlanningResult resultToReturn = Mapper.Map<GetPlanningResult_v3, FWSGetPlanningResult>(result);



执行上面的代码行是给我的映射异常错误。下面的异常的详细信息:

Executing the above line of code is giving me the mapping exception error. Below the details of the exception:

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: 

我希望有人可以给我的解决方案。至于你可以在对象的数量看..我没那么渴望开始手工绘制的一切。

I hope that someone might give me the solution.. As you can see on the number of objects.. I'm not that eager to start mapping everything manually.

谢谢!

推荐答案

我的问题已经解决。问题是以下内容:

My problem has been fixed. The problem was the following:

在我们的解决方案,我们最近开始与国际奥委会的工作。有人创建了一个与TypeAdapter作为AutoMapperTypeAdapter引用的实体。

In our solution we recently started working with IOC. Someone created a TypeAdapter with AutoMapperTypeAdapter as referenced entity.

当我们的应用程序某个模块被打开或进入,Automapper正在重新初始化,并且在Global.asax中创建了映射被拆除。

When in our application a certain module was open or entered, Automapper was being initialized again and my mappings created in global.asax were removed.

修改我的代码也使用TypeAdapter下班后,我的问题是固定的。

After modifying my code to also work using the TypeAdapter, my problem was fixed.

谢谢对于万万想帮助我!

Thanks for everying trying to help me!

这篇关于AutoMapper.AutoMapperMappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆