我想使用AutoMapper 3將具有Integer屬性的類投影到具有String屬性的另一個類。如何使用AutoMapper 3和實體框架將Integer映射到字符串
當執行查詢然後我得到以下異常:
System.NotSupportedException:LINQ到實體無法識別方法「System.String的ToString()」方法,而這種方法不能被翻譯成商店表達。
這裏是代碼的相關部分:
public partial class Lookup
{
public int LookupId { get; set; }
public int LookupTypeId { get; set; }
public string Value { get; set; }
public int SequencialOrder { get; set; }
public virtual LookupType LookupType { get; set; }
}
public class LookupProfile : Profile
{
protected override void Configure()
{
CreateMap<Lookup, SelectListItem>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.LookupId.ToString()))
.ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Value));
}
}
和查詢的樣子:
Provinces = _db.Lookups.Project().To<SelectListItem>().ToList()
問:
有沒有一種方法,我可以配置LookupProfile到做適當的映射,並仍然在Linq To Entities中工作? 或者還有另外一種方法可以使投影與Linq to Entities一起工作嗎?
Can not'SelectListItem.Value'是一個整數嗎? –
我的想法完全一樣,但事實證明它不能在這種情況下。我找到了解決方案,並回答了問題 – Elie