여기와 automapper 위키에서 수많은 예제를 시도했지만 여전히이 문제를 해결할 수 없습니다. 중첩 된 개체와 중첩 된 컬렉션을 매핑하려고하는데 내가 무엇을하든 항상 오류가 발생합니다. 컨트롤러가 데이터를 반환하도록 할 수있는 유일한 방법은 두 속성에 대해 option.ignore를 설정하는 것입니다.
매핑하려는 비즈니스 계층 개체입니다.
public class LocationBL
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public string Country { get; set; }
public DbGeography Coordinates { get; set; }
public int LocationType_Id { get; set; }
public virtual LocationTypeBL LocationType { get; set; }
public virtual ICollection<SportBL> Sports { get; set; }
}
public class LocationTypeBL
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<LocationBL> Locations { get; set; }
}
public class SportBL
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<LocationBL> Locations { get; set; }
public virtual ICollection<UserBL> Users { get; set; }
}
데이터 영역 개체입니다.
public class Location : EntityData
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Company")]
public int? CompanyId { get; set; }
[Required]
public string Name { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public string Country { get; set; }
[Required]
public DbGeography Coordinates { get; set; }
[ForeignKey("LocationType")]
public int LocationType_Id { get; set; }
public virtual LocationType LocationType { get; set; }
public virtual ICollection<Sport> Sports { get; set; }
public virtual Company Company { get; set; }
}
public class LocationType : EntityData
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
public class Sport : EntityData
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Location> Locations { get; set; }
public virtual ICollection<User> Users { get; set; }
}
이것은 내 매핑 프로필입니다.
public class LocationProfile : Profile
{
public LocationProfile()
{
CreateMap<LocationType, LocationTypeBL>();
CreateMap<LocationTypeBL, LocationType>();
CreateMap<Location, LocationBL>()
.ForMember(Dest => Dest.Sports,
opt => opt.MapFrom(src => src.Sports))
.ForMember(Dest => Dest.LocationType,
opt => opt.MapFrom(src => src.LocationType));
CreateMap<LocationBL, Location>()
.ForMember(Dest => Dest.Sports,
opt => opt.MapFrom(src => src.Sports))
.ForMember(Dest => Dest.LocationType,
opt => opt.MapFrom(src => src.LocationType));
}
}
업데이트 ******* 이것은 내 LocationType 프로필입니다.
public class LocationTypeProfile : Profile
{
public LocationTypeProfile()
{
CreateMap<LocationType, LocationTypeBL>();
CreateMap<LocationTypeBL, LocationType>();
}
}
이것은 내 스포츠 프로필입니다
public class SportProfile : Profile
{
public SportProfile()
{
CreateMap<Sport, SportBL>();
CreateMap<SportBL, Sport>();
}
}
중요한지는 확실하지 않지만 이것은 Autofac, WebAPI 및 OWIN을 사용하는 Azure Mobile App 백엔드입니다. AutoMapper와 Autofac을 사용하는 것은 이번이 처음이므로 아직 배우는 중이므로 용서해주십시오. 프로필이 모두 등록되어 있고 중첩 된 개체를 무시하도록 설정하면 컨트롤러가 적절한 데이터를 반환합니다.
미리 감사드립니다 !!!
거의 다 왔습니다. 중첩 된 개체를 매핑하는 방법도 AutoMapper에 지시해야합니다. 당신은에 대한지도를 작성해야합니다 그래서 Sport
에를 SportBL
그 반대도합니다.
// use ForMember if needed, but you know how to do that so I won't
// show it.
CreateMap<Sport, SportBL>();
그러면 AutoMapper는 중첩 된 복합 유형을 매핑 할 때 해당 매핑을 사용합니다.
또 다른 참고 사항은 클래스에 동일한 속성이있는 경우 ReverseMap()
메서드를 호출하기 만하면 양방향 매핑이 수행됩니다.
그래서이 대신 :
CreateMap<LocationType, LocationTypeBL>();
CreateMap<LocationTypeBL, LocationType>();
동일한 작업을 수행하기 위해 이렇게 할 수 있습니다.
Mapper.CreateMap<LocationType, LocationTypeBL>().ReverseMap();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다