AutoMapper 8.0缺少GetPropertyMaps [英] AutoMapper 8.0 missing GetPropertyMaps

查看:100
本文介绍了AutoMapper 8.0缺少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'的定义,并且找不到可访问的扩展方法'GetPropertyMaps'接受类型为'TypeMap'的第一个参数(您是否缺少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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆