简体   繁体   English

C#AutoMapper - Inhertaince mapping - 简单的例子

[英]C# AutoMapper - Inhertaince mapping - simple example

A few days ago I asked a question for Mapping and I Familiar to Automapper then I can successfully Map Class B to A . 几天前我问了一个关于Mapping和我熟悉Automapper的问题然后我可以成功地将B映射A Now I have another problem with inheritance. 现在我有继承的另一个问题。

I have four Classes. 我有四个课程。

Class A: A类:

public class A : BaseViewModel {
    prop string p1  { get; set; }
    prop string p2  { get; set; }
    prop string p3  { get; set; }
}

Class BaseViewModel: 类BaseViewModel:

public class BaseViewModel {
    prop int Id {get; set;}
    prop string SomeP1 {get; set;}
}

Class B: B级:

public class B : BaseModel {
    prop string p1  { get; set; }
    prop string p3  { get; set; }
}

Class BaseModel: 类BaseModel:

public class BaseModel {
    prop int Id {get; set;}
    prop string SomeP2 {get; set;}
}

Mapper: 制图员:

Mapper.CreateMap<B, A>();
A a_obj= Mapper.Map<A>(b_obj);

My question is : How can I map Id in BaseViewModel to Id in BaseModel ? 我的问题是:我如何映射IdBaseViewModelIdBaseModel

I found a solution but It's a little difficult for me. 我找到了一个解决方案,但这对我来说有点困难。

You can use this: 你可以用这个:

 Mapper.Initialize(cfg => cfg.CreateMap<B, A>());
 var a_obj = Mapper.Map<A>(b_obj);

Tip : You have to use automapper v6.* 提示:您必须使用automapper v6。*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM