호 조건이 무시되는 것처럼 보입니다. 여기 내 시나리오 :오토 마퍼의 조건이 무시됩니다.
소스 클래스
public class Source
{
public IEnumerable<Enum1> Prop1{ get; set; }
public IEnumerable<Enum2> Prop2{ get; set; }
public IEnumerable<Enum3> Prop3{ get; set; }
}
바이트에서 열거 서브 클래스 및 [플래그]로 장식되어 있습니다. 대상 클래스에는 단순히 "전체"비트 값을 포함하는 Enum1, Enum2 및 Enum3과 같은 속성이 포함됩니다. Enumeration에 Enum1.value!, Enum1.Value2 및 Enum1.Value3이 포함되어있는 경우 Essense에서는 대상에 Enum1.Value1 비트 값이 포함됩니다. Enum1.Value2 | 내부 속성이 null이며, 매핑이 성공하고 정확하게 목적지를 설정할 때 Enum1.Value3
대상 클래스
public Enum1 Prop1 { get; set; }
public Enum2 Prop2 { get; set; }
public Enum3 Prop3 { get; set; }
AutoMapper 매핑
Mapper.CreateMap<Source, Destination>()
.ForMember(m => m.Prop1, o =>
{
o.Condition(c => !c.IsSourceValueNull);
o.MapFrom(f => f.Prop1.Aggregate((current, next) => current | next));
})
.ForMember(m => m.Prop2, o =>
{
o.Condition(c => !c.IsSourceValueNull);
o.MapFrom(f => f.Prop2.Aggregate((current, next) => current | next));
})
.ForMember(m => m.Prop3, o =>
{
o.Condition(c => !c.IsSourceValueNull);
o.MapFrom(f => f.Prop3.Aggregate((current, next) => current | next));
});
매핑은 잘 작동합니다. 그러나 멤버 원본 값이 null 인 경우 매핑을 건너 뛰고 싶습니다 (Prop1이 null 인 경우 매핑을 건너 뜁니다).
디버깅에서 Source.Prop1이 null임을 알 수 있습니다. 조건이 완전히 무시되고 값이 null이라는 예외가 발생합니다.
Trying to map Source to Destination. Destination property: Prop1. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. --> Value cannot be null. Parameter name: source
IsSourceValueNull이 Prop1인지 또는 실제 소스 클래스가 null이 아닌지 여부는 확실하지 않습니다. 구성원 Prop1 만 null입니다.
도움이 필요합니다.
감사합니다. 패트릭. 나는 이것을 분리함으로써 똑같은 예외를 얻는다. – TimJohnson
나는 전에 그 일을했다고 생각했다. 가능한 한 가지는 c.IsSourceValueNull은 아마도 매핑 될 전체 소스 객체가 null인지 여부를 나타냅니다. 그것이 "c => c.Prop1! = null"이어야하는지 궁금해? – PatrickSteele
"조건"을 사용하여 코드 샘플을 찾을 수 없습니다. 죄송합니다. 당신이 항상 할 수있는 것 같아요 : o.MapFrom (f => f.Prop1 == null? null : f.Prop1.Aggregate (...)) – PatrickSteele