Automapper 3.3.0から6.0.2にアップグレードしたばかりで、ソリューションをアップグレードした後、マッピングの1つがもう機能していないようです。Automapper - オーバーライドされたプロパティをマッピングするとキャストが発生する例外
この考えてみましょう:私は新しい「DerivedDestinationMaster」インスタンスに「ソース」オブジェクトをマップしようとした場合、私が得る、
今CreateMap<Source,DestinationMaster>()
.Include<DerivedSource,DerivedDestinationMaster>();
CreateMap<DerivedSource,DerivedDestinationMaster>()
.ForMember(dest => dest.Child, act=> act.MapFrom(src => new DerivedDestinationChild(){ GrossAmount = src.GrossAmount }));
:マッピングの
public class Source{
public Decimal GrossAmount {get;set;}
}
public class DerivedSource : Source{
}
public class DestinationMaster{
public virtual DestinationChild Child {get;set;}
}
public class DestinationChild{
public decimal GrossAmount {get;set;}
}
public class DerivedDestinationMaster : DestinationMaster {
private DerivedDestinationChild _destinationChild;
public override DestinationChild Child {
get{return _destinationChild;}
set{_destinationChild = (DerivedDestinationChild)value;}
}
}
public class DerivedDestinationChild : DestinationChild{
public string OtherProperty {get;set;}
}
そして今を"DestinationChild"を "DerivedDestinationChild"に変換できないという例外があります。
"setter"アクセサのオーバーライドプロパティにブレークポイントを設定すると、渡されたインスタンスのタイプが "DestinationChild"であることがわかります。 "DestinactionChild"クラスのコンストラクタコールが2つあることもわかります。 "GrossAmount"値は、 "DerivedDestinationChild"を作成してインスタンス化した後、 "DestinationChild"の新しいインスタンスを作成して情報を受け取って2つのオブジェクトをオートマッシュするかのように、0ではなく "Source"オブジェクトの良い値です。
私たちのシナリオはもう少し複雑ですが(私たちはIValueResolverを使用します)、 "MapFrom"、 "UseValue"とカスタムリゾルバでテストしました。最終的なインスタンスは派生型ではありません。
我々はマッピングに「act.Ignoreを()」を入れた場合は、例外が
を提起されていないがあなたの助けをありがとう!