オートマッパーでマッピングする継承を使用する複雑なオブジェクトがあります。get リクエストでは完全にマッピングされますが、ポスト リクエストでは、まったく同じコードが初期化された型を正しくマッピングしません。
説明させてください。(以下のコードを参照)
単純な get リクエスト中にオブジェクトをマップする最初のケースでは、完全に正常にマップされ、以下Parent
のクラスのプロパティはA
その特定のタイプB
またはC
.
しかし、投稿中にまったく同じマッピングが発生した場合、のParent
プロパティのA
型はA
!?? です。
これで、コードは同じになり、DB から返されるデータ モデルも同じになります。(私は nhibernate を使用しています - タイプは期待どおりです) 唯一の違いは、それがポスト リクエストであることです。
この場合、AutoMapper について知っておくべきことはありますか?
クラス定義 (ViewModel は同じ構造に従います):
public class A
{
public A Parent { get; set;}
}
public class B : A
{ }
public class C : A
{ }
そして、次のようにマッピングされます:
CreateMap<A, AViewModel>()
.Include<B, BViewModel>()
.Include<C, CViewModel>();
CreateMap<B, BViewModel>();
CreateMap<C, CViewModel>();
呼び出しマップ:
var aModel = _aManager.Get("same parameter");
var aViewModel = Mapper.Map<AViewModel>(aModel);
in
編集 #1 - これは、投稿アクションのロジックを示しています。
[Transaction] // Commits the nhibernate transaction on OnActionExecuted
[HttpPost]
public ActionResult UpdateA(OtherModelViewModel viewModel)
{
var a = _aManager.Get("same parameter");
var otherModel = Mapper.Map<OtherModel>(viewModel);
a.AddOtherModel(otherModel);
_otherModelRepository.New(otherModel);
// Eeek, writing this out I am seeing a problem here, I suspect this is where my problem would be, loading the model again from the db, after updating it in session without commiting it? I am going to change the logic and see if it fixes it.
var aModel = _aManager.Get("same parameter");
var aViewModel = Mapper.Map<AViewModel>(aModel);
// return result.
}