Automapper IList - 方法实现中的主体签名和声明不匹配
我在应用程序层中定义了此映射:
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;
}
专业实体
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; }
}
}
专业DTO:
public class ProfessionDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
执行GetAllProfessions时,我收到此错误:
签名方法实现中的主体和声明不匹配。
知道为什么会发生这种情况吗?
我刚刚将所有 IList 更改为 List。我现在没有收到异常,但检索到的 27 个 Profession 实体的列表被映射到 ProfessionDTO 的 0。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我觉得回答自己的问题很愚蠢。
我不需要这一行:
现在 Auomaper 工作完美!
I feel silly answering my own question.
I don't need this line:
Now Auomapper works perfectly!
您的职业类别中没有 Id 和 Name 属性的设置器。
you don't have setters for your Id and Name attribute in your profession class.
给出的答案似乎是错误的;
正确的应该是这样的;
The answer indicated seems wrong;
Correct one should be like;