AutoMapper 8.0 missing GetPropertyMaps

南楼画角 提交于 2020-02-02 05:48:08

问题


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;
    }

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"

After upgrading to AutoMapper 8.0 I got this error at build:

'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?)

What are replacements for GetPropertyMaps in AutoMapper 8.0?

Thanks!


回答1:


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 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 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;
    }


来源:https://stackoverflow.com/questions/53371494/automapper-8-0-missing-getpropertymaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!