Error using Automapper " Class Not Found"

Asked

Viewed 561 times

0

I’m studying the use of AutoMapper, but I get an error in my project.

Automapper version: v3.3.1

I added the Config in my Global.asax, ficando assim:

 protected void Application_Start()
        {
            AutoMapperConfig.RegisterMappings();
        }

My AutoMapperConfig is this way:

public class AutoMapperConfig
    {
        public static void RegisterMappings()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile<DomainToViewModelMappingProfile>();
                cfg.AddProfile<ViewModelToDomainMappingProfile>();
            });
        }
    }

My class DomainToViewModelMappingProfile is this way:

public class DomainToViewModelMappingProfile : Profile
    {
        public override string ProfileName
        {
            get { return "DomainToViewModelMappings"; }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<Pessoa, PessoaViewModel>();
            Mapper.CreateMap<Endereco, EnderecoViewModel>();
            Mapper.CreateMap<Pessoa, PessoaEnderecoViewModel>();
            Mapper.CreateMap<Endereco, PessoaEnderecoViewModel>();
        }
    }

and the ViewModelToDomainMappingProfile is like this:

public class ViewModelToDomainMappingProfile : Profile
    {
        public override string ProfileName
        {
            get { return "ViewModelToDomainMappings"; }
        }

        protected override void Configure()
        {
            Mapper.CreateMap<PessoaViewModel, Pessoa>();
            Mapper.CreateMap<EnderecoViewModel, Endereco>();
            Mapper.CreateMap<PessoaEnderecoViewModel, Pessoa>();
            Mapper.CreateMap<PessoaEnderecoViewModel, Endereco>();
        }
    }

When "mapping" the class in my AppService thus:

 public void Adicionar(PessoaEnderecoViewModel pessoaEnderecoViewModel)
    {

        var pessoa = Mapper.Map<PessoaEnderecoViewModel, Pessoa>(pessoaEnderecoViewModel);
        var endereco = Mapper.Map<PessoaEnderecoViewModel, Endereco>(pessoaEnderecoViewModel);

        pessoa.Enderecos.Add(endereco);
        _pessoaRepository.Add(pessoa);
    }

When debugging, when I get to this part var pessoa = Mapper.Map(pessoaEnderecoViewModel); I get the following error:

Mapper.Cs not found

Locating source for 'c:\dev\AutoMapper\src\AutoMapper\Mapper.cs'. Checksum: MD5 {28 13 88 2d fa c bf 90 0 c9 9c 25 64 11 3d 36}
The file 'c:\dev\AutoMapper\src\AutoMapper\Mapper.cs' does not exist.
Looking in script documents for 'c:\dev\AutoMapper\src\AutoMapper\Mapper.cs'...
Looking in the projects for 'c:\dev\AutoMapper\src\AutoMapper\Mapper.cs'.
The file was not found in a project.
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src\vccorlib\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\mfc\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\atl\'...
Looking in directory 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\include'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: c:\dev\AutoMapper\src\AutoMapper\Mapper.cs.
The debugger could not locate the source file 'c:\dev\AutoMapper\src\AutoMapper\Mapper.cs'.

And appears the option to locate the file Mapper.Cs.

I tried to follow the example of of this answer, but it didn’t help.

1 answer

0


I got it here, I was checking the error in the wrong place.

The problem was in my class Pessoa. I own a ICollection . When starting it in the constructor, it worked perfectly.

public Pessoa()
        {
            Enderecos = new List<Endereco>();
        }

Browser other questions tagged

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