AutoMapper 8.0 缺少 GetPropertyMaps [英] AutoMapper 8.0 missing GetPropertyMaps
问题描述
在 AutoMapper 8.0 之前,我使用此代码通过字符串查找属性映射,例如:实体模型具有名为currency_id"的属性,而 DTO 具有名为currency"的属性.我在 AutoMapper 中定义了双向映射,我用这段代码来查找源/目标属性相关
Prior to AutoMapper 8.0, I used this code to find a property mapping by string, example: entity model has property named "currency_id" and DTO has property named "currency". I have defined bi-directional mapping in AutoMapper, and I used this code to find source/target property relat
public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty)
{
var mapper = AutoMapper.IMapper.ConfigurationProvider;
// TSrc = source generic type
// TDst = destination generic type
var map = mapper.FindTypeMapFor<TSrc, TDst>();
var propertyMap = map.GetPropertyMaps()
.FirstOrDefault(pm =>
pm.SourceMember.Name == sourceProperty
);
return propertyMap.DestinationProperty.Name;
}
在 AutoMapper 配置文件中:
In AutoMapper Profile:
this.CreateMap<EntityModels.contact, DTO.contact>()
.ForMember(m => m.currency, src => src.MapFrom(f => f.currency_id))
;
this.CreateMap<DTO.contact, EntityModels.contact>()
.ForMember(m => m.currency_id, src => src.MapFrom(f => f.currency))
;
当我这样调用我的方法时:
When I called my method like this:
var _dboField = GetDestinationPropertyFor<DTO.contact, EntityModels.contact>(this.mapper, "currency");
Console.WriteLine(_dboField);
// output should be "currency_id"
升级到 AutoMapper 8.0 后,我在构建时遇到此错误:
After upgrading to AutoMapper 8.0 I got this error at build:
TypeMap"不包含GetPropertyMaps"的定义,并且找不到接受TypeMap"类型的第一个参数的可访问扩展方法GetPropertyMaps"(您是否缺少 using 指令或程序集引用?)
'TypeMap' does not contain a definition for 'GetPropertyMaps' and no accessible extension method 'GetPropertyMaps' accepting a first argument of type 'TypeMap' could be found (are you missing a using directive or an assembly reference?)
AutoMapper 8.0 中 GetPropertyMaps 的替代品是什么?
What are replacements for GetPropertyMaps in AutoMapper 8.0?
谢谢!
推荐答案
正如 Lucian 所建议的,MemberMaps 是一个可能的替代品.但是,PropertyMaps 与 AutoMapper 7.0 中的 GetPropertyMaps 完全相同.DestinationProperty
也被重命名为 DestinationMember
.
As Lucian suggested, MemberMaps is a possible replacement. However, PropertyMaps does exactly the same as GetPropertyMaps in AutoMapper 7.0. DestinationProperty
has also been renamed to DestinationMember
.
AutoMapper 7.0 代码:
AutoMapper 7.0 code:
public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty)
{
var mapper = AutoMapper.IMapper.ConfigurationProvider;
// TSrc = source generic type
// TDst = destination generic type
var map = mapper.FindTypeMapFor<TSrc, TDst>();
var propertyMap = map.GetPropertyMaps()
.FirstOrDefault(pm =>
pm.SourceMember.Name == sourceProperty
);
return propertyMap.DestinationProperty.Name;
}
AutoMapper 8.0 代码:
AutoMapper 8.0 code:
public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty)
{
var mapper = AutoMapper.IMapper.ConfigurationProvider;
// TSrc = source generic type
// TDst = destination generic type
var map = mapper.FindTypeMapFor<TSrc, TDst>();
var propertyMap = map.PropertyMaps
.FirstOrDefault(pm =>
pm.SourceMember.Name == sourceProperty
);
return propertyMap.DestinationMember.Name;
}
这篇关于AutoMapper 8.0 缺少 GetPropertyMaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!