3

I'm update my automapper 4.2.1 from nuget and it was install automapper 6.1.1 version. In old version all mapping works properly but when I update my automapper library mapping didn't work it says that Missing type map configuration or unsupported mapping. Mapping types when use Mapper.Map<Class1,Class2>(result). I'm using profile to configure in global.asax. My project is C# MVC and here is my codes;

In Global.asax

 public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
           AutoMapperConfigurations.Configure();
        }
    }

My Mapper configuration

public class AutoMapperConfigurations
    {
        public static void Configure()
        {

            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile<ModeltoDTOMappingsProfile>();
                cfg.AddProfile<DTOtoDTOModelMappingsProfile>();
                cfg.AddProfile<DTOtoViewModelMappingsProfile>();
                //cfg.ForAllMaps((tm, me) => me.ForAllMembers(option => option.Condition((source, destination, sourceMember) => sourceMember != null)));
            });


            Mapper.AssertConfigurationIsValid();
   }

Here is my one profile example code

public class DTOtoViewModelMappingsProfile: Profile
    {

        public DTOtoViewModelMappingsProfile()
        {

            Mapper.Initialize(cfg =>
            {
                // Also I tried these one but It didn't work

                //cfg.CreateMissingTypeMaps = true;
                //cfg.AllowNullCollections = true;
                //cfg.AllowNullDestinationValues = false;
                //cfg.ForAllMaps((tm, me) => me.ForAllMembers(option => option.Condition((source, destination, sourceMember) => sourceMember != null)));

    #region Category

                cfg.CreateMap<TreeCategoryViewModel, TreeCategoryDtos>()
                    .ForMember(dest => dest.T_C_ID, opt => opt.MapFrom(src => src.CId))
                    .ForMember(dest => dest.T_C_DESC, opt => opt.MapFrom(src => src.CDesc))
                    .ForMember(dest => dest.T_C_INUSE, opt => opt.MapFrom(src => src.CInuse))
                    .ForMember(dest => dest.T_IS_TECH, opt => opt.MapFrom(src => src.IsTech))
                    .ForMember(dest => dest.T_IS_BRANCH, opt => opt.MapFrom(src => src.IsBranch))
                    .ForMember(dest => dest.T_IS_MARKETING, opt => opt.MapFrom(src => src.IsMarketing))
                    .ForMember(dest => dest.T_IS_SUPPORT, opt => opt.MapFrom(src => src.IsSupport))
                    .ForMember(dest => dest.T_C_LEVEL, opt => opt.MapFrom(src => src.CLevel))
                    .ForMember(dest => dest.T_C_ORDER, opt => opt.MapFrom(src => src.COrder))
                    .ForMember(dest => dest.T_C_PARENT, opt => opt.MapFrom(src => src.CParent))
                    .ForMember(dest => dest.TREE_CATEGORY1, opt => opt.ResolveUsing(src => src.TreeCategoryDtos1))
                    .ForMember(dest => dest.TREE_CATEGORY2, opt => opt.ResolveUsing(src => src.TreeCategoryDtos2))
                    ;

                cfg.CreateMap<TreeCategoryDtos, TreeCategoryViewModel>()
                    .ForMember(dest => dest.CId, opt => opt.MapFrom(src => src.T_C_ID))
                    .ForMember(dest => dest.CDesc, opt => opt.MapFrom(src => src.T_C_DESC))
                    .ForMember(dest => dest.CInuse, opt => opt.MapFrom(src => src.T_C_INUSE))
                    .ForMember(dest => dest.IsTech, opt => opt.MapFrom(src => src.T_IS_TECH))
                    .ForMember(dest => dest.IsBranch, opt => opt.MapFrom(src => src.T_IS_BRANCH))
                    .ForMember(dest => dest.IsMarketing, opt => opt.MapFrom(src => src.T_IS_MARKETING))
                    .ForMember(dest => dest.IsSupport, opt => opt.MapFrom(src => src.T_IS_SUPPORT))
                    .ForMember(dest => dest.CLevel, opt => opt.MapFrom(src => src.T_C_LEVEL))
                    .ForMember(dest => dest.COrder, opt => opt.MapFrom(src => src.T_C_ORDER))
                    .ForMember(dest => dest.CParent, opt => opt.MapFrom(src => src.T_C_PARENT))
                    .ForMember(dest => dest.TreeCategoryDtos1, opt => opt.ResolveUsing(src => src.TREE_CATEGORY1))
                    .ForMember(dest => dest.TreeCategoryDtos2, opt => opt.ResolveUsing(src => src.TREE_CATEGORY2))
                    ;

                #endregion

            });

            Mapper.AssertConfigurationIsValid();
        }

In Controller

var result = Mapper.Map<TreeCategoryViewModel,TreeCategoryDtos>(treeCategories);

It was exception in this line. Why I'm getting this error?.(Missing type map configuration or unsupported mapping. Mapping types: TreeCategoryViewModel -> TreeCategoryDtos Project.Models.ViewModel.TreeCategoryViewModel -> Project.BusinessLayer.CategoriesBL.Dtos.TreeCategoryDtos )


Now the error message has changed. This is what I'm getting now " Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance."

But the error comes from this class.In my model mapping.

public class ModeltoDTOMappingsProfile : Profile
    {
        public ModeltoDTOMappingsProfile()
        {
                 CreateMap<TREE_CATEGORY, TreeCategoryDtos>();
                 CreateMap<TreeCategoryDtos, TREE_CATEGORY>()
                    .ForMember(dest => dest.G_MARK_FOR_DELETE, opt => opt.Ignore())
                    .ForMember(dest => dest.L_INSERTDATE, opt => opt.Ignore())
                    .ForMember(dest => dest.L_INSERTUSER, opt => opt.Ignore())
                    .ForMember(dest => dest.L_UPDATEDATE, opt => opt.Ignore())
                    .ForMember(dest => dest.L_UPDATEUSER, opt => opt.Ignore());

        }
    }

I'm using AutomapperConfigurations to call my profile in global.asax. You can see these code above

Bora Oktekin
  • 33
  • 1
  • 6

1 Answers1

1

You must only call Mapper.Initialize once - with this code, each time you load a profile you are overwriting the configuration.

Your profiles should inherit from Profile - see the documentation

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I edit my question to add new error message. For new error I have to use Dependency Injection to create mapping properly? – Bora Oktekin Jun 30 '17 at 14:44
  • No, you don't have to use DI. Looking at your question it looks like what the docs suggest. However the error message mentions _" make sure you do not have any calls to the static Mapper.Map methods,"_ - have you checked that? – stuartd Jun 30 '17 at 15:28
  • I checked that and change it to Mapper.Instance.Map but I got same error. In this scenario how can I use Map ? – Bora Oktekin Jun 30 '17 at 16:01
  • @stuatd could you please have a look https://stackoverflow.com/questions/73744463/automapper-upgrade-to-6-1-0-assertconfigurationisvalid-fails-in-servicestack – A_m0 Sep 16 '22 at 17:39