.net core AutoMapper 8.0缺少GetPropertyMaps

.net core AutoMapper 8.0缺少GetPropertyMaps,.net-core,automapper,.net Core,Automapper,在AutoMapper 8.0之前,我使用此代码通过字符串查找属性映射,例如:实体模型的属性名为“currency_id”,DTO的属性名为“currency”。我在AutoMapper中定义了双向映射,并使用此代码查找源/目标属性relat public static string GetDestinationPropertyFor<TSrc, TDst>(IMapper IMapper, string sourceProperty) { var m

在AutoMapper 8.0之前,我使用此代码通过字符串查找属性映射,例如:实体模型的属性名为“currency_id”,DTO的属性名为“currency”。我在AutoMapper中定义了双向映射,并使用此代码查找源/目标属性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;
    }
公共静态字符串GetDestinationPropertyFor(IMapper IMapper,string sourceProperty)
{
var mapper=AutoMapper.IMapper.ConfigurationProvider;
//TSrc=源泛型类型
//TDst=目标泛型类型
var map=mapper.FindTypeMapFor();
var propertyMap=map.GetPropertyMaps()
.FirstOrDefault(pm=>
pm.SourceMember.Name==sourceProperty
);
返回propertyMap.DestinationProperty.Name;
}
在自动映射配置文件中:

        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))
        ;
this.CreateMap()
.ForMember(m=>m.currency,src=>src.MapFrom(f=>f.currency\u id))
;
这个
.ForMember(m=>m.currency\u id,src=>src.MapFrom(f=>f.currency))
;
当我这样调用我的方法时:

var _dboField = GetDestinationPropertyFor<DTO.contact, EntityModels.contact>(this.mapper, "currency");

Console.WriteLine(_dboField);
// output should be "currency_id"
var\u dboField=GetDestinationPropertyFor(this.mapper,“currency”);
控制台写入线(_dboField);
//输出应为“货币id”
升级到AutoMapper 8.0后,我在生成时遇到以下错误:

“TypeMap”不包含“GetPropertyMaps”的定义,并且找不到接受“TypeMap”类型的第一个参数的可访问扩展方法“GetPropertyMaps”(是否缺少using指令或程序集引用?)

AutoMapper 8.0中的GetPropertyMaps有哪些替代品


谢谢

正如Lucian所建议的,MemberMaps是一个可能的替代品。但是,PropertyMaps的功能与AutoMapper 7.0中的GetPropertyMaps完全相同
DestinationProperty
也被重命名为
DestinationMember

AutoMapper 7.0代码:

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;
    }
公共静态字符串GetDestinationPropertyFor(IMapper IMapper,string sourceProperty)
{
var mapper=AutoMapper.IMapper.ConfigurationProvider;
//TSrc=源泛型类型
//TDst=目标泛型类型
var map=mapper.FindTypeMapFor();
var propertyMap=map.GetPropertyMaps()
.FirstOrDefault(pm=>
pm.SourceMember.Name==sourceProperty
);
返回propertyMap.DestinationProperty.Name;
}
AutoMapper 8.0代码:

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;
    }
公共静态字符串GetDestinationPropertyFor(IMapper IMapper,string sourceProperty)
{
var mapper=AutoMapper.IMapper.ConfigurationProvider;
//TSrc=源泛型类型
//TDst=目标泛型类型
var map=mapper.FindTypeMapFor();
var propertyMap=map.propertyMap
.FirstOrDefault(pm=>
pm.SourceMember.Name==sourceProperty
);
返回propertyMap.DestinationMember.Name;
}

但您为什么需要该目的地属性?也许有更好的方法来做你想做的事情。这是一个解决OData bug的方法。API接受DTO中的属性名称等参数,但必须将其“反映”到实体模型中。例如:$orderby=currency应该构建类似.orderby(o=>o.currency\u id)的表达式。我已经这样做了,问题是Automapper中缺少通过映射表达式实现的功能。你所做的是一个黑客。MemberMaps是您想要的。但事实上,这些代码并不是解决问题的方法。谢谢。你能给我一个方向吗?我应该寻找什么?正如您可能在源代码中看到的,成员映射还包括路径映射和构造函数映射。所以,如果不使用ForPath和构造函数映射,就不需要它。