AutoMapper.AutoMapperMappingException: Missing type mapping or unsupported mapping

2

I'm using the AutoMapper library to map my ViewModels.

I have my DBdomain class:

public class DBDominio
    {

        [Key]
        [Column("ID")]
        public long id { get; set; }

        [Required]
        [StringLength(128)]
        [Column("ID_USU")]
        public string idUsu { get; set; }

        [Required]
        [StringLength (200)]
        [Column("URL")]
        public string url { get; set; }

        [StringLength (100)]
        [Column("ID_c")]
        public string idC { get; set; } 

        [StringLength(100)]
        [Column("ID_s")]
        public string idS { get; set; } 
    }

And my viewModel VMDomainDetails:

public class VMDominioDetails
    {
        public long id { get; set; }

        public string idUsu { get; set; }

        public string url { get; set; }

        public string idC { get; set; } 

        public string idS { get; set; } 
    }

In my Global.asax I added the AutoMapperWebConfiguration.Configure ();

My configuration class looks like this:

AutoMapperWebConfiguration

public static class AutoMapperWebConfiguration
    {

        public static void Configure()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile(new DominioProfile());

            });

            Mapper.AssertConfigurationIsValid();
        }
    }

And my DomainProfile class:

public class DominioProfile : Profile
    {
        protected override void Configure()
        {

            Mapper.CreateMap<DBDominio, VMDominioDetails>();

        }
    }

When trying to "map", I get the following error:

  

Missing type mapping or unsupported mapping.

     

Mapping types: DBDomain - > VMDomainDetails   PROJECT.Models.DBModels.DBDomain - >   PROJECT.Models.ViewModels.VMDomainDetails

     

Destination path: VMDomainDetails

     

Source value: PROJETO.Models.DBModels.DBDomain

     

Description: An unhandled exception occurred during the execution of the   current Web request. Examine the stack trace for   more information about the error and where it originated in the code.

     

Exception Details: AutoMapper.AutoMapperMappingException: Missing   type map configuration or unsupported mapping.

     

Mapping types: DBDomain - > VMDomainDetails   PROJECT.Models.DBModels.DBDomain - >   PROJECT.Models.ViewModels.VMDomainDetails

     

Destination path: VMDomainDetails

     

Source value: PROJETO.Models.DBModels.DBDomain

The error happens on this line, where dBDominio is an object of type DBDominio

VMDominioDetails d = new VMDominioDetails();
d = _map.Map<VMDominioDetails>(dBDominio);

I have tried to use a reverse reference in the configuration to see if it would solve, thus: Mapper.CreateMap<DBDominio, VMDominioDetails>().ReverseMap() , but not resolved.

    
asked by anonymous 15.10.2015 / 18:07

2 answers

1

Ricardo I ended up doing a test and found that DominioProfile has to call CreateMap like this:

public class DominioProfile : Profile
{
     [Obsolete]
     protected override void Configure()
     {
         CreateMap<DBDominio, VMDominioDetails>();            
     }
}

then CreateMap is a method of Profile , but, also another problem, this way is obsoleto , then you should change to this:

public class AutoMapperWebConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<DBDominio, VMDominioDetails>();

        });

        Mapper.AssertConfigurationIsValid();
    }
}

by putting direct in AutoMapperWebConfiguration .

In the code of controller done:

VMDominioDetails d = new VMDominioDetails();
d = Mapper.Map<VMDominioDetails>(new DBDominio
    {
       id = 100,
       idC = "idc"
    });

and the fields and their values have been correctly mapped.

Just to end in Global.asax , I made the call AutoMapperWebConfiguration.Configure(); :

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AutoMapperWebConfiguration.Configure();
    }
}
    
14.10.2016 / 02:27
0

Try replacing the line:

d = _map.Map<VMDominioDetails>(dBDominio);

by:

Mapper.Map(dBDominio,d);
    
26.03.2016 / 23:48