2016-08-12 2 views
0

AutoMapper를 사용하고 내 모델 (EntityFramework 사용)에 DateTime 속성이 필요하지만 POCO에서 Nullable datetimes를 사용하는 상황이 있습니다.날짜/시간 - NULL 입력 가능 날짜/시간 변환기 자동 완성

ItemModel.OtherDate.Year==1900ItemPOCO.MyDatenull이어야합니다.

다른 속성 이름을 사용하여 DateTime을 Nullable DateTime으로 변환하는 방법은 무엇입니까?

나는이 시도했지만 작동하지 않습니다 : Property 'Int32 Year' is not defined for type 'System.Nullable'1[System.DateTime]'

// Class Model (EntityFramework) 
public class ItemModel{ 
    public DateTime OtherDate { get; set; } 
} 

// POCO 
public class ItemPOCO{ 
    public DateTime? MyDate { get; set; } 
} 

// AutoMapper Profile 
public class MappingProfile : Profile 
{ 
    public MappingProfile() 
    { 
    CreateMap<ItemModel, ItemPOCO>() 
     .ForMember(poco => poco.MyDate, 
       opt => opt.MapFrom(m => m.OtherDate.Year == 1900 ? null : new DateTime?(m.OtherDate))); 
    } 
} 

Error message

+1

코드를 복사하면 잘 작동합니다 .. –

+0

@ MarkC. 당신 말이 맞아요! AutoMapper 5.0.2에서 문제가 발생했습니다. 버전 5.1.1로 업데이트했고 괜찮습니다. :) –

답변

1

AutoMapper 5.0.2에서 발생했습니다 문제입니다. 버전 5.1.1로 업데이트되었고 괜찮습니다 :)