小编Kle*_*ezk的帖子

ASP.NET Core 5 和 EF - InvalidOperationException

此查询的范围,它显示按类别 > 主菜单 > 子菜单分组的导航菜单。

在 LINQPad 中工作正常,但 Web 应用程序向我显示以下未处理的错误:

InvalidOperationException:无法在投影中转换集合子查询,因为父查询不会投影在客户端生成结果所需的所有表的关键列。当尝试关联无键实体或使用“Distinct”或“GroupBy”操作而不投影所有键列时,可能会发生这种情况。

询问:

var mainMenu = (from c in _unitOfWork.Menu.GetAllQueryable()
                        where c.MenuPadreId == 0
                        select new
                        {
                            Categoria = c.MenuCategoria.Descripcion,
                            CategoriaId = c.MenuCategoriaId
                        }).Distinct();

        var menuNavegacion = from c in mainMenu
                             select new MenuNavegacionCategoriaViewModel
                             {
                                 Categoria = c.Categoria,
                                 CategoriaId = c.CategoriaId,
                                 MenuNavegacionViewModel = (from m in _unitOfWork.Menu.GetAllQueryable()
                                                            where m.MenuPadreId == 0
                                                                  && m.MenuCategoriaId == c.CategoriaId
                                                            select new MenuNavegacionViewModel
                                                            {
                                                                MenuId = m.MenuId,
                                                                MenuPadreId = m.MenuPadreId,
                                                                Descripcion = m.Descripcion,
                                                                Area = m.Area, …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework asp.net-core

7
推荐指数
2
解决办法
9385
查看次数

实体框架repostory模式GetAll()太慢了

我正在使用存储库层.我的问题是,当连接具有大记录的表时,GetAll()方法太慢.运行简单查询需要40秒.

IGenericRepository:

public interface IGenericRepository<TEntity>
{
    TEntity FindBy(Expression<Func<TEntity, bool>> predicate);
    IEnumerable<TEntity> GetAll();
    TEntity GetById(int id);
    TEntity Insert(TEntity entity);
    TEntity Update(TEntity entity);
    void Delete(object id);
    void Save();
}
Run Code Online (Sandbox Code Playgroud)

GenericRepository:

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    private MyStoreContext _dbContext;
    protected DbSet<TEntity> DbSet;

    public GenericRepository()
    {
        _dbContext = new MyStoreContext ();
        DbSet = _dbContext.Set<TEntity>();
    }

    public TEntity FindBy(Expression<Func<TEntity, bool>> predicate)
    {
        return DbSet.Where(predicate).SingleOrDefault();
    }

    public IEnumerable<TEntity> GetAll()
    {
        return DbSet.AsNoTracking();
    }

    public TEntity GetById(int id)
    {
        return DbSet.Find(id);
    } …
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc entity-framework-6

0
推荐指数
1
解决办法
856
查看次数