SQLite 查询中“不支持应用联接”

Mar*_*arc 6 c# sql-server sqlite select include

我正在尝试将我的应用程序与 MS SQL Server 2014 数据库转换为 SQlite。该查询在 SQL Server 上运行良好,但使用 SQLite 时,我遇到“不支持 APPLY JOINS”错误。

此错误仅存在于 *select (& include) 查询中。

询问:

        public static IList<Projet> GetListByClientWithDetails(long IdClient)
    {
        IList<Projet> resultList = null;

        using (FITSEntities db_context = new FITSEntities())
        {
            resultList = db_context.Projet.Where(s => s.IdClient == IdClient)
                .Include(s => s.Cdts.Select(r => r.CdtFiches))
                .Include(s => s.Cdts.Select(r => r.Sessions))
                .Include(s => s.Fiches.Select(r => r.FicheVersions))
                .ToList();
        }
        return resultList;
    }
Run Code Online (Sandbox Code Playgroud)

如果我评论这一行:.Include(s => s.Cdts.Select(r => r.CdtFiches))

        public static IList<Projet> GetListByClientWithDetails(long IdClient)
    {
        IList<Projet> resultList = null;

        using (FITSEntities db_context = new FITSEntities())
        {
            resultList = db_context.Projet.Where(s => s.IdClient == IdClient)
              //  .Include(s => s.Cdts.Select(r => r.CdtFiches))
                .Include(s => s.Cdts.Select(r => r.Sessions))
                .Include(s => s.Fiches.Select(r => r.FicheVersions))
                .ToList();
        }
        return resultList;
    }
Run Code Online (Sandbox Code Playgroud)

效果很好。

如果我评论另一行:.Include(s => s.Cdts.Select(r => r.Sessions))

        public static IList<Projet> GetListByClientWithDetails(long IdClient)
    {
        IList<Projet> resultList = null;

        using (FITSEntities db_context = new FITSEntities())
        {
            resultList = db_context.Projet.Where(s => s.IdClient == IdClient)
                .Include(s => s.Cdts.Select(r => r.CdtFiches))
               // .Include(s => s.Cdts.Select(r => r.Sessions))
                .Include(s => s.Fiches.Select(r => r.FicheVersions))
                .ToList();
        }
        return resultList;
    }
Run Code Online (Sandbox Code Playgroud)

它也运作良好。

sqlite select 查询有什么具体规则吗?

Art*_*out 2

我知道这是一个旧线程,但我今天遇到了它,所以这是我对未来读者的两分钱

这种有点不寻常的错误要么是由于 SQLite 数据库的工作方式造成的,要么是由于 EF 的 SQLite 提供程序的编写方式造成的。

无论哪种情况,为了简单地“使查询工作”而解决这个问题的希望都很渺茫。

不过,我发现有一个解决方法可以解决这个问题。虽然它可能没有利用 EF 的强大功能,但它可以完成工作。

问题的核心

主要问题是此 LINQ 查询尝试Include在同一个一对多表(在您的情况下为 )上使用两个一对多导航属性Cdts

当尝试Include“多个级别”时,在纯 LINQ 使用中执行此操作的一种方法Include是抛出Select

resultList = db_context.Projet.Where(s => s.IdClient == IdClient)
    .Include(s => s.Cdts.Select(r => r.CdtFiches))
    .Include(s => s.Cdts.Select(r => r.Sessions))
Run Code Online (Sandbox Code Playgroud)

在这里,我想您想包括表中的一对多关系CdtFiches和。然而 SQLite 不喜欢这样(不过我不知道为什么,因为 SQLServer 对此很好)。SessionsCdt

您需要做的是手动创建Select根实体并使用 强制获取相关实体ToList。这达到了完全相同的结果Include(尽管我怀疑后者更有效)。

在你的情况下

resultList = db_context.Projet.Where(s => s.IdClient == IdClient)
    .Select(_toDeepProjet)
    .ToList()
    
private Projet _toDeepProjet(Projet p)
{
    p.Cdts = p.Cdts.Select(_toDeepCdts).ToList();
    return p;
}

private Cdts _toDeepCdts(Cdts c)
{
    // Force the fetching of entities
    // It is equivalent to writing two Includes in your original query
    c.CdtFiches = c.CdtFiches.ToList();
    c.Sessions = c.Sessions.ToList();
    return c;
}
Run Code Online (Sandbox Code Playgroud)

这很黑客。但它有效。