将集合的属性映射到数组会引发NotSupportedException

时间:2017-01-03 09:18:54

标签: c# entity-framework automapper

我在web api项目中使用Automapper 5.2.0和EF 6.1.3。

public class PushNotification
{
    public int[] Stores { get; set; }
}

public class PushNotificationEntity
{
    [Key]
    public int Id { get; set; }
    public ICollection<StoreEntity> Stores { get; set; }
}

public class StoreEntity
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<PushNotificationEntity> PushNotifications { get; set; }
}

这是映射,

cfg.CreateMap<PushNotificationEntity, PushNotification>()
                .ForMember(dest => dest.Stores,
                            opts => opts.MapFrom(src => src.Stores.Select(s => s.Id).ToArray()));

这是我访问它的方式,

var item = ctx.PushNotifications.Include(p => p.Stores)
            .Where(n => n.Id == id).ProjectTo<PushNotification>(mapperCfg).SingleOrDefault();

我收到了以下错误,

System.NotSupportedException.
LINQ to Entities does not recognize the method 'Int32[]
ToArray[Int32](System.Collections.Generic.IEnumerable`1[System.Int32])'
method, and this method cannot be translated into a store expression.

0 个答案:

没有答案