Automapper Problem- 목록은 맵핑되지 않습니다
-
20-09-2019 - |
문제
다음 수업이 있습니다.
public class Account
{
public int AccountID { get; set; }
public Enterprise Enterprise { get; set; }
public List<User> UserList { get; set; }
}
그리고 나는 다음과 같은 방법 조각이 있습니다.
Entities.Account accountDto = new Entities.Account();
DAL.Entities.Account account;
Mapper.CreateMap<DAL.Entities.Account, Entities.Account>();
Mapper.CreateMap<DAL.Entities.User, Entities.User>();
account = DAL.Account.GetByPrimaryKey(this.Database, primaryKey, withChildren);
Mapper.Map(account,accountDto);
return accountDto;
메소드가 호출되면 계정 클래스가 올바르게 매핑되지만 계정 클래스의 사용자 목록은 그렇지 않습니다 (NULL). 목록에 매핑 해야하는 4 개의 사용자 엔티티가 있습니다. 누군가가 무엇이 잘못되었는지 말해 줄 수 있습니까?
해결책
계정에 전달하지 말고 Automapper가 당신을 위해 그것을 만들도록하십시오. 기존 대상 객체에 매핑되면 Automapper는 몇 가지 가정을 만듭니다. 대신 :
var accountDto = Mapper.Map<DAL.Entities.Account, Entities.Account>(account);
마지막으로 확인해야 할 것은 구성이 유효하다는 것입니다.
Mapper.AssertConfigurationIsValid();
그 createmap 호출 후. 이를 통해 모든 것이 대상 유형의 사물에 올바르게 일치하는지 확인합니다.
제휴하지 않습니다 StackOverflow