C# &引用;不支持应用联接";在SQLite查询中
我正在尝试将使用MS SQL Server 2014数据库的应用程序转换为SQlite。 此查询在SQL Server上运行良好,但在使用SQLite时,我遇到了“不支持应用连接”错误 此错误仅存在于*select(&include)查询中 查询:C# &引用;不支持应用联接";在SQLite查询中,c#,sql-server,sqlite,select,include,C#,Sql Server,Sqlite,Select,Include,我正在尝试将使用MS SQL Server 2014数据库的应用程序转换为SQlite。 此查询在SQL Server上运行良好,但在使用SQLite时,我遇到了“不支持应用连接”错误 此错误仅存在于*select(&include)查询中 查询: public static IList<Projet> GetListByClientWithDetails(long IdClient) { IList<Projet> resultL
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;
}
公共静态IList GetListByClientWithDetails(长IdClient)
{
IList resultList=null;
使用(FITSEntities db_context=new FITSEntities())
{
resultList=db_context.Projet.Where(s=>s.IdClient==IdClient)
.Include(s=>s.Cdts.Select(r=>r.cdt文件))
.Include(s=>s.Cdts.Select(r=>r.Sessions))
.Include(s=>s.Fiches.Select(r=>r.FicheVersions))
.ToList();
}
返回结果列表;
}
如果我对这行进行注释:.Include(s=>s.Cdts.Select(r=>r.CdtFiches))
公共静态IList GetListByClientWithDetails(长IdClient)
{
IList resultList=null;
使用(FITSEntities db_context=new FITSEntities())
{
resultList=db_context.Projet.Where(s=>s.IdClient==IdClient)
//.Include(s=>s.Cdts.Select(r=>r.cdt文件))
.Include(s=>s.Cdts.Select(r=>r.Sessions))
.Include(s=>s.Fiches.Select(r=>r.FicheVersions))
.ToList();
}
返回结果列表;
}
它工作得很好
如果我评论另一行:.Include(s=>s.Cdts.Select(r=>r.Sessions))
公共静态IList GetListByClientWithDetails(长IdClient)
{
IList resultList=null;
使用(FITSEntities db_context=new FITSEntities())
{
resultList=db_context.Projet.Where(s=>s.IdClient==IdClient)
.Include(s=>s.Cdts.Select(r=>r.cdt文件))
//.Include(s=>s.Cdts.Select(r=>r.Sessions))
.Include(s=>s.Fiches.Select(r=>r.FicheVersions))
.ToList();
}
返回结果列表;
}
它也很有效
sqlite select查询有什么特定的规则吗?我知道这是一个旧线程,但我今天就遇到了它,所以这是我给未来读者的两分钱
这个有点非常规的错误要么是由于SQLite数据库的工作方式,要么是由于为EF编写SQLite提供程序的方式
在这两种情况下,为了简单地“使查询工作”,几乎不可能解决这个问题
然而,我发现有一个解决方法可以绕过这个问题。虽然它可能没有利用EF的强大功能,但它完成了任务
问题的核心
主要问题是,此LINQ查询试图在同一个一对多表上包含两个一对多导航属性(在您的示例中是,Cdts
)
在“多个级别”上尝试Include
时,使用Include
在纯LINQ中执行此操作的一种方法是插入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))
在这里,我假设您希望包括Cdt文件
和会话
,它们是Cdt
表上的一对多关系。但是SQLite不喜欢这一点(我不知道为什么,因为SQLServer可以使用它)
您需要做的是手动选择
您的根实体,并使用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;
}
它是黑的。但它是有效的
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))
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;
}