IdentityManager icon indicating copy to clipboard operation
IdentityManager copied to clipboard

Automapper about to deprecate Mapper.CreateMap if you upgrade to latest

Open sksallaj82 opened this issue 8 years ago • 2 comments

In the file: api/Models/UserQueryResultsResource.cs

I get an error saying that Mapper.CreateMap will be deprecated and removed in future versions of AutoMapper.

Mapper.CreateMap<TSource,TDestination> is obsolete: Dynamically creating maps will be removed in 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize(). Use CreateMapper to create a mapper instance.

I've actually fixed this on my end. I'm not sure how to pull code and merge it into a newer version of this, so I'll detail the steps here. When you do it for that, you can also follow the same steps for RoleQueryResultResource.cs, adding the mappings to the AutoMapperWebConfigurations.cs (described below)

I created the following classes:

In api/Models/UserQueryResultsResource.cs

public class UserResourceProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<QueryResult<UserSummary>, UserResultResourceList>()
             .ForMember(x => x.Items, opts => opts.MapFrom(x => x.Items));
        CreateMap<UserSummary, UserResultResource>()
            .ForMember(x => x.Data, opts => opts.MapFrom(x => x))
            .ForMember(x => x.Links, opts => opts.Ignore());
    }
}`

public class UserResultResourceList
{
    public IEnumerable<UserResultResource> Items { get; set; }
}

In Configurations folder:

public static class AutoMapperWebConfigurations
{
    public static IMapper Mapper;
    public static void Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new UserResourceProfile());
        });
        Mapper = config.CreateMapper();
        config.AssertConfigurationIsValid();
    }
}

In api/Models/UserQueryResultsResouce.cs replace the lines in UserQueryResultResourceData :

Mapper.Map(result, this);

with:

UserResultResourceList userResourceList = AutoMapperWebConfigurations.Mapper.Map<QueryResult<UserSummary>, UserResultResourceList>(result);
this.Items = userResourceList.Items;

finally, add Global.asax

    protected void Application_Start()
    {
        AutoMapperWebConfigurations.Configure();
    }

sksallaj82 avatar Mar 17 '16 20:03 sksallaj82

I fixed .. the following class public class UserQueryResultResourceData : QueryResult<UserSummary>

public static readonly IMapper _userQueryConfig;

replace the

old

static UserQueryResultResourceData() { AutoMapper.Mapper.CreateMap<QueryResult<UserSummary>, UserQueryResultResourceData>() .ForMember(x => x.Items, opts => opts.MapFrom(x => x.Items)); AutoMapper.Mapper.CreateMap<UserSummary, UserResultResource>() .ForMember(x => x.Data, opts => opts.MapFrom(x => x)); }

new

    static UserQueryResultResourceData()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<QueryResult<UserSummary>, UserQueryResultResourceData>()
              .ForMember(x => x.Items, opts => opts.MapFrom(x => x.Items));

            cfg.CreateMap<UserSummary, UserResultResource>()
            .ForMember(x => x.Data, opts => opts.MapFrom(x => x));

        });
        _userQueryConfig = config.CreateMapper();
    }

and finally replaced with with AutoMapper.Mapper.Map(result, this); ---> _userQueryConfig.Map(result, this);

do same for rolequesry as well

vijay2358 avatar Dec 13 '16 20:12 vijay2358

@vijay2358 Thanks for the fix! Had the same issue... was not blocking me, however quite annoying. Updated automapper in IdentityManager project changed the code and it works!

AlexLiakh avatar Jul 27 '17 02:07 AlexLiakh