使用Linq OrderBy子屬性錯誤的Automapper Projection

automapper entity-framework-core linq-to-sql projection

我使用AutoMapper(版本5.1.1)投影結合Linq OrderBy Child屬性表達式時遇到問題。我正在使用Entity Framework Core(版本1.0.0)。我收到以下錯誤:

“必須是可簡化的節點”

我的DTO對像如下

public class OrganizationViewModel
    {
        public virtual int Id { get; set; }
        [Display(Name = "Organization Name")]
        public virtual string Name { get; set; }
        public virtual bool Active { get; set; }
        public virtual int OrganizationGroupId { get; set; }
        public virtual string OrganizationGroupName { get; set; }
        public virtual int StrategyId { get; set; }
        public virtual string StrategyName { get; set; }
        public virtual OrganizationGroupViewModel OrganizationGroup { get; set; }
    }

public class OrganizationGroupViewModel
    {
        public virtual int Id { get; set; }
        [Display(Name = "Organization Group Name")]
        public virtual string Name { get; set; }
        public virtual bool Active { get; set; }
    }

我對應的實體模型如下:

public class Organization
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string TimeZone { get; set; }
        public bool Active { get; set; }
        //FKs
        public int OrganizationGroupId { get; set; }
        public int StrategyId { get; set; }
        //Navigation
        public virtual OrganizationGroup OrganizationGroup { get; set; }
        public virtual Strategy Strategy { get; set; }
        [JsonIgnore]
        public virtual List<AppointmentReminder> AppointmentReminders { get; set; }
    }

public class OrganizationGroup
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Active { get; set; }

        public virtual List<Organization> Organizations { get; set; }
    }

我的AutoMapper配置文件如下:

public class OrganizationMapperProfile : Profile
    {
        public OrganizationMapperProfile()
        {
            CreateMap<Task<Organization>, Task<OrganizationViewModel>>();
            CreateMap<Organization, OrganizationViewModel>()
                .ForMember(dest => dest.OrganizationGroupName, opt => opt.MapFrom(src => src.OrganizationGroup.Name));
            CreateMap<OrganizationInput, Organization>()
                .ForMember(x => x.Id, opt => opt.Ignore());
        }
    }

public class OrganizationGroupMapperProfile : Profile
    {
        public OrganizationGroupMapperProfile()
        {
            CreateMap<Task<OrganizationGroup>, Task<OrganizationGroupViewModel>>();
            CreateMap<OrganizationGroup, OrganizationGroupViewModel>();
            CreateMap<OrganizationGroupInput, OrganizationGroup>()
                .ForMember(x => x.Id, opt => opt.Ignore());
        }
    }

當我運行以下語句時,我能夠運行並從前兩個語句中獲得結果:

var tmp = await _context.Organizations.Include(x => x.OrganizationGroup).OrderBy(x => x.OrganizationGroup.Name).ToListAsync();
var tmp4 = await _context.Organizations.Include(x => x.OrganizationGroup).OrderBy("OrganizationGroup.Name").ToListAsync();

但是當我添加ProjectTo時,我得到上面列出的錯誤:

var tmp5 = await _context.Organizations.Include(x => x.OrganizationGroup).OrderBy(x => x.OrganizationGroup.Name).ProjectTo<OrganizationViewModel>().ToListAsync();
var tmp6 = await _context.Organizations.Include(x => x.OrganizationGroup).OrderBy("OrganizationGroup.Name").ProjectTo<OrganizationViewModel>().ToListAsync();

作為一些額外的信息,我能夠使用Project處理父類的屬性,例如:

var tmp7 = await _context.Organizations.Include(x => x.OrganizationGroup).OrderBy(x => x.Name).ProjectTo<OrganizationViewModel>().ToListAsync();
var tmp8 = await _context.Organizations.Include(x => x.OrganizationGroup).OrderBy("Name").ProjectTo<OrganizationViewModel>().ToListAsync();

有人遇到過這個問題嗎?看起來我正在嘗試做一些其他方面不支持的東西,那是設計的嗎?感謝您的幫助/見解。

一般承認的答案

看起來這個問題是由OrganizationViewModel類的OrganizationGroup屬性引起的 - AutoMapper生成一個空檢查,EF Core在與OrderBy的組合中不喜歡(我想這只是EF Core中當前的許多錯誤之一)。它可以通過以下簡單的手動投影查詢輕鬆複製:

var tmp5a = _context.Organizations
    .OrderBy(x => x.OrganizationGroup.Name)
    .Select(e => new OrganizationViewModel
    {
        Id = e.Id,
        OrganizationGroup = e.OrganizationGroup != null ? new OrganizationGroupViewModel
        {
             Id = e.OrganizationGroup.Id,
             Name = e.OrganizationGroup.Name,
             Active = e.OrganizationGroup.Active,
        } : null,
    })
    .ToList();

要解決此問題,請將AutoMapper配置為不對該屬性生成null檢查,如下所示:

CreateMap<Organization, OrganizationViewModel>()
    .ForMember(dest => dest.OrganizationGroup, opt => opt.AllowNull())
    .ForMember(dest => dest.OrganizationGroupName, opt => opt.MapFrom(src => src.OrganizationGroup.Name));


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因