zoukankan      html  css  js  c++  java
  • Entity Framework Core的坑,Select后再对导航属性进行查询或Select前进行Skip/Take

    把asp.net core的项目发布到ubuntu上了,运行的时候出现了如下警告:

    warn: Microsoft.EntityFrameworkCore.Query[20500]
    The LINQ expression 'where [g.ResBaseInfo].ResType.Equals(Hotel)' could not be translated and will be evaluated locally.
    warn: Microsoft.EntityFrameworkCore.Query[20500]
    The LINQ expression 'Skip(__p_3)' could not be translated and will be evaluated locally.
    warn: Microsoft.EntityFrameworkCore.Query[20500]
    The LINQ expression 'Take(__p_4)' could not be translated and will be evaluated locally.
    warn: Microsoft.EntityFrameworkCore.Query[10106]
    The Include operation for navigation '[g].ResBaseInfo' is unnecessary and was ignored because the navigation is not reachable in the final query results. See https://go.microsoft.com/fwlink/?linkid=850303 for more information.
    warn: Microsoft.EntityFrameworkCore.Query[20500]
    The LINQ expression 'where [g.ResBaseInfo].ResType.Equals(Hotel)' could not be translated and will be evaluated locally.
    warn: Microsoft.EntityFrameworkCore.Query[20500]
    The LINQ expression 'Count()' could not be translated and will be evaluated locally.

    本来打算不理会的,但是仔细看了下,大概的意思是会把所有数据拉到本地再进行查询处理,如果数据量大了,服务器肯定遭不住,还是分析代码找到了原因。

    错误在于先在一个公共方法里select出了基础数据,然后在Controller中再次根据条件进行where,如果where的是导航属性中的属性,就会报上面的警告。修改了代码,把对导航属性的where语句放到select前,问题解决。

            public IQueryable<ResBaseInfoView> GetResBaseInfoViewQueryable(Lang lang, ResType resType = 0)
            {
                var jquery = _context.ResBaseInfoMultipleLanguage
                    //.Include(g => g.ResBaseInfo)
                    //.ThenInclude(g => g.AreaCode)
                    .Where(g => !g.ResBaseInfo.IsDelete && g.Language.Equals(lang));
    
                if (resType != 0)
                {
                    jquery = jquery.Where(g => g.ResBaseInfo.ResType == resType);
                }
    
                var result = jquery
                    .Select(rbml => new ResBaseInfoView
                    {
                        ID = rbml.ResBaseInfoID,
                        AddAccountID = rbml.ResBaseInfo.AddAccountID,
                        AreaCodeID = rbml.ResBaseInfo.AreaCodeID,
                        IsDelete = rbml.ResBaseInfo.IsDelete,
                        Language = rbml.Language,
                        MLID = rbml.MLID,
                        UpdateTime = rbml.ResBaseInfo.UpdateTime,
                        ImageName = rbml.ResBaseInfo.ImageName,
                        Name = rbml.Name,
                        ResType = rbml.ResBaseInfo.ResType,
                        ResTypeName = rbml.ResBaseInfo.ResType.GetDescription(lang),
                        AreaName = rbml.ResBaseInfo.AreaCode.GetML_Name(lang),
                        Address = rbml.Address,
                        AddTime = rbml.ResBaseInfo.AddTime,
                        Lng = rbml.ResBaseInfo.Lng,
                        Lat = rbml.ResBaseInfo.Lat,
                        ZoomLevel = rbml.ResBaseInfo.ZoomLevel,
                        IsShow = rbml.ResBaseInfo.IsShow,
                        MobilePhone = rbml.MobilePhone,
                        Telephone = rbml.Telephone,
                        Sequence = rbml.ResBaseInfo.Sequence
                    });
    
                return result;
            }
    

      

  • 相关阅读:
    Codeforces Round #652 (Div. 2) A. FashionabLee(几何)
    轻量应用服务器如何通过修改apache配置文件实现非https的访问多域名到不同子目录程序?
    在Windows环境下使用hexo搭建博客以及部署到gitee / github
    使用WordPress搭建个人手机博客(阿里云)
    访问自己服务器的ip地址
    php环境无法上传文件的解决方法
    SSRF漏洞
    CSRF全家桶(含义,防御,攻击)
    JS实现HTML实体与字符的相互转换
    CentOS系统下载及应用部署
  • 原文地址:https://www.cnblogs.com/fireicesion/p/10901167.html
Copyright © 2011-2022 走看看