Automapper IList - Signature of the body and declaration in a method implementation do not match
Asked Answered
G

3

6

I have this mapping defined in my Application Layer:

public IList<ProfessionDTO> GetAllProfessions()
{
    IList<Profession> professions = _professionRepository.GetAll();
    Mapper.CreateMap<Profession, ProfessionDTO>();
    Mapper.CreateMap<IList<Profession>, IList<ProfessionDTO>>();
    IList<ProfessionDTO> professionsDto = Mapper.Map<IList<Profession>, IList<ProfessionDTO>>(professions);
    return professionsDto;
}

Proffesion entity

 public class Profession
    {
        private int _id;
        private string _name;


        private Profession(){} // required by nHibernate

        public Profession(int id, string name)
        {
            ParameterValidator.NotNull(id, "id is required.");
            ParameterValidator.NotNull(name, "name is required.");
            _id = id;
            _name = name;
        }

        public string Name
        {
            get { return _name; }
        }

        public int Id
        {
            get { return _id; }
        }
    }

Profession DTO:

public class ProfessionDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
}

When executing GetAllProfessions I get this error:

Signature of the body and declaration in a method implementation do not match.

Any idea why is it happening?

I have just changed all the IList to List. I don't get the exception now but the List of 27 entities of Profession that is retrieved is mapped to 0 of ProfessionDTO.

Gingergingerbread answered 17/1, 2012 at 10:52 Comment(0)
G
12

I feel silly answering my own question.

I don't need this line:

Mapper.CreateMap<IList<Profession>, IList<ProfessionDTO>>();

Now Auomapper works perfectly!

Gingergingerbread answered 17/1, 2012 at 11:17 Comment(0)
F
0

you don't have setters for your Id and Name attribute in your profession class.

Filippa answered 17/1, 2012 at 11:2 Comment(2)
It shouldn't matter. Profession entity is the source. Setters are never used.Gingergingerbread
Yeah, I know, but it is the only thing I see different between those properties..Filippa
T
0

The answer indicated seems wrong; Correct one should be like;

Mapper.CreateMap<Profession, ProfessionDTO>();
Trimorphism answered 26/7, 2015 at 4:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.