Automappermappingexception: Missing type map Configuration or Unsupported Mapping

Asked

Viewed 5,346 times

2

I’m using the library Automapper to map my Viewmodels.

I own my class Dbdominio:

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 Vmdominiodetails:

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 call Automapperwebconfiguration.Configure();

My configuration class is this way:

Automapperwebconfiguration

public static class AutoMapperWebConfiguration
    {

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

            });

            Mapper.AssertConfigurationIsValid();
        }
    }

And my class Dominioprofile:

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

            Mapper.CreateMap<DBDominio, VMDominioDetails>();

        }
    }

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

Missing type map Configuration or Unsupported Mapping.

Mapping types: Dbdomain -> Vmdominiodetails PROJECT.Models.Dbmodels.Dbdomain -> PROJETO.Models.Viewmodels.Vmdominiodetails

Destination path: Vmdominiodetails

Source value: PROJETO.Models.Dbmodels.Dbdominio

Description: An untreated exception occurred during the execution of current web request. Examine stack tracking to get 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 -> Vmdominiodetails PROJECT.Models.Dbmodels.Dbdomain -> PROJETO.Models.Viewmodels.Vmdominiodetails

Destination path: Vmdominiodetails

Source value: PROJETO.Models.Dbmodels.Dbdominio

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

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

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

2 answers

2

Ricardo I ended up doing a test and found that in DominioProfile have to call CreateMap thus:

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

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

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

        });

        Mapper.AssertConfigurationIsValid();
    }
}

putting right into the AutoMapperWebConfiguration.

In the code of controller done:

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

and has been correctly mapped the fields and their respective values.

Just to end on Global.asax, 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();
    }
}

0

Try to replace the line:

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

for:

Mapper.Map(dBDominio,d);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.