2016-08-12 60 views
0

我使用AutoMapper,我有我的模型(使用EntityFramework)需要日期時間屬性,但我的POCO使用可空日期時間的情況。日期時間到可空日期時間轉換器自動映射器

ItemModel.OtherDate.Year==1900ItemPOCO.MyDate應該是null

任何方式將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,它很好:)