AutoMapper - 从 v8 升级到 v9 后不使用泛型
AutoMapper - Not working with generics after upgrade from v8 to v9
在我们更新到版本 9 之前,我有可能和奇怪的 AutoMapper 设置正在运行。
我们从数据库中获取数据,将其包装在通用对象中,然后将其映射到 DTO:
//build some models
Domain test1 = new Domain() { Id = 1, Name = "Shaun" };
Domain test2 = new Domain() { Id = 2, Name = "Dave" };
//this is what I get from my API
Revision<Domain> preMap = new Revision<Domain>() {
RevisionNumber = 2,
Items = new System.Collections.Generic.List<Domain> {
test1, test2,
}
};
//build mapper obj
var config = new MapperConfiguration(cfg => {
cfg.AddMaps(typeof(MyMaps).Assembly);
});
Mapper mapper = new Mapper(config);
//map!
var postMap = mapper.Map<Revision<DTOHistory<Dto>>>(preMap);
Console.ReadLine();
这是我的修改和映射配置文件类:
public class Revision<MyItem> {
public long RevisionNumber { get; set; }
public List<MyItem> Items { get; set; }
}
public class MyMaps : AutoMapper.Profile {
public MyMaps() {
CreateAPI<Domain, Dto>();
}
private void CreateAPI<H, T>() {
//I've tried fiddling with these to get a good config
CreateMap<Revision<H>, Revision<DTOHistory<T>>>(MemberList.None);
//.ForMember(dest => dest.Items, opt => opt.MapFrom(src => src.Items))
//.ReverseMap();
CreateMap<H, DTOHistory<T>>(MemberList.None);
//.ReverseMap();
}
}
我要么以 NULL DTO 结尾,要么我收到类似于以下内容的错误:
Mapping types:
Revision`1 -> Revision`1
AutoMapperTest.Revision`1[[AutoMapperTest.Domain, AutoMapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> AutoMapperTest.Revision`1[[AutoMapperTest.DTOHistory`1[[AutoMapperTest.Dto, AutoMapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], AutoMapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]```
Lucian Bargaoanu 的评论显示了问题:
http://docs.automapper.org/en/latest/9.0-Upgrade-Guide.html
AutoMapper no longer creates maps automatically (CreateMissingTypeMaps
and conventions) You will need to explicitly configure maps, manually
or using reflection. Also consider attribute mapping.
为了解决这个问题,我需要检查每个导航属性并为链中的每个类型创建一个地图。
在我们更新到版本 9 之前,我有可能和奇怪的 AutoMapper 设置正在运行。
我们从数据库中获取数据,将其包装在通用对象中,然后将其映射到 DTO:
//build some models
Domain test1 = new Domain() { Id = 1, Name = "Shaun" };
Domain test2 = new Domain() { Id = 2, Name = "Dave" };
//this is what I get from my API
Revision<Domain> preMap = new Revision<Domain>() {
RevisionNumber = 2,
Items = new System.Collections.Generic.List<Domain> {
test1, test2,
}
};
//build mapper obj
var config = new MapperConfiguration(cfg => {
cfg.AddMaps(typeof(MyMaps).Assembly);
});
Mapper mapper = new Mapper(config);
//map!
var postMap = mapper.Map<Revision<DTOHistory<Dto>>>(preMap);
Console.ReadLine();
这是我的修改和映射配置文件类:
public class Revision<MyItem> {
public long RevisionNumber { get; set; }
public List<MyItem> Items { get; set; }
}
public class MyMaps : AutoMapper.Profile {
public MyMaps() {
CreateAPI<Domain, Dto>();
}
private void CreateAPI<H, T>() {
//I've tried fiddling with these to get a good config
CreateMap<Revision<H>, Revision<DTOHistory<T>>>(MemberList.None);
//.ForMember(dest => dest.Items, opt => opt.MapFrom(src => src.Items))
//.ReverseMap();
CreateMap<H, DTOHistory<T>>(MemberList.None);
//.ReverseMap();
}
}
我要么以 NULL DTO 结尾,要么我收到类似于以下内容的错误:
Mapping types:
Revision`1 -> Revision`1
AutoMapperTest.Revision`1[[AutoMapperTest.Domain, AutoMapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> AutoMapperTest.Revision`1[[AutoMapperTest.DTOHistory`1[[AutoMapperTest.Dto, AutoMapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], AutoMapperTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]```
Lucian Bargaoanu 的评论显示了问题:
http://docs.automapper.org/en/latest/9.0-Upgrade-Guide.html
AutoMapper no longer creates maps automatically (CreateMissingTypeMaps and conventions) You will need to explicitly configure maps, manually or using reflection. Also consider attribute mapping.
为了解决这个问题,我需要检查每个导航属性并为链中的每个类型创建一个地图。