Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有基类和不同配置选项的Automapper实现_C#_.net_Automapper - Fatal编程技术网

C# 带有基类和不同配置选项的Automapper实现

C# 带有基类和不同配置选项的Automapper实现,c#,.net,automapper,C#,.net,Automapper,我有两个类(MVC视图模型),它们继承自一个抽象基类 abstract class BaseModel { } class Car : BaseModel { public string Speed { get; set; } } class Camper : BaseModel { public int Beds { get; set; } } 并希望使用基类配置AutoMapper,例如: Mapper.CreateMap<BaseModel, DataDes

我有两个类(MVC视图模型),它们继承自一个抽象基类

abstract class BaseModel { }

class Car : BaseModel 
{
    public string Speed { get; set; }
}

class Camper : BaseModel
{
    public int Beds { get; set; } 
}
并希望使用基类配置AutoMapper,例如:

Mapper.CreateMap<BaseModel, DataDestination>();

var someObj = new DataDastination();
Mapper.Map(instanceOfBaseModel, someObj);
Mapper.CreateMap();
var someObj=新的DataDastination();
Map(instanceOfBaseModel,someObj);
这里我得到了错误,因为Automapper并没有汽车或露营车的配置。尝试使用以下内容配置Automapper:

Mapper.CreateMap<BaseModel, DataDestination>()
    .ForMember(dest => dest.SomeProp, mapper => mapper.MapFrom( .... ));
Mapper.CreateMap()
.ForMember(dest=>dest.SomeProp,mapper=>mapper.MapFrom(..);

在MapFrom中,我只看到基类的属性!如何配置Automapper以使用汽车和露营车的基类和特定FormMember表达式?例如,如果它是一辆汽车,请从此处映射此属性,如果它是露营车,请从其他地方映射此属性

下面是描述的主题

以下内容适用于您:

Mapper.CreateMap<BaseModel, DataDastination>()
    .Include<Car, DataDastination>()
    .Include<Camper, DataDastination>();//.ForMember(general mapping)
Mapper.CreateMap<Car, DataDastination>();//.ForMember(some specific mapping)
Mapper.CreateMap<Camper, DataDastination>();//.ForMember(some specific mapping)
Mapper.CreateMap()
.包括()
.Include();/。FormMember(通用映射)
Mapper.CreateMap();/。FormMember(某些特定映射)
Mapper.CreateMap();/。FormMember(某些特定映射)
使用.includealderived()

Mapper.CreateMap().includealderived()
CreateMap();
CreateMap();

在上解释的类似情况下不起作用。有什么想法吗?你救了我一天:)可耻的是,我一开始并不总是参考文档。这是一个很好的例子来说明我为什么应该这样做。谢谢。”.includealderived()“正是我想要的!
Mapper.CreateMap<BaseModel, DataDestination>().IncludeAllDerived()
Mapper.CreateMap<Car, DataDestination>();
Mapper.CreateMap<Camper, DataDestination>();