Linq OrderBy子属性错误

时间:2016-10-10 01:13:26

标签: linq-to-sql automapper entity-framework-core 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();

之前有人遇到过这个问题吗?看起来我正在尝试做一些其他方面不支持的东西,那是设计的吗?感谢您的帮助/见解。

1 个答案:

答案 0 :(得分:4)

看起来问题是由OrganizationGroup类的OrganizationViewModel属性引起的 - AutoMapper生成一个空检查,EF Core与您的OrderBy组合不喜欢(I猜测只是目前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));