- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 Entity Framework 作为我的 ORM 对两个 SQL 函数进行连接。执行查询时,我收到此错误消息:
The query attempted to call 'Outer Apply' over a nested query,
but 'OuterApply' did not have the appropriate keys
这是我的查询:
var ingredientAllergenData = (from ings in db.fnListIngredientsFromItem(productId, (short)itemType, productId)
join ingAllergens in db.fnListAllergensFromItems(productId.ToString(CultureInfo.InvariantCulture), (short)itemType, currentLang)
on ings.id equals ingAllergens.ingredientId into ingAllergensData
from allergens in ingAllergensData.DefaultIfEmpty()
where ings.table == "tblIng" || ings.table == ""
select new {ings, allergens}).ToList();
我在 LINQPad 中编写了相同的查询并返回了结果,所以我不确定问题出在哪里:
var ingredientAllergenData = (from ings in fnListIngredientsFromItem(1232, 0, 1232)
join ingAllergens in fnListAllergensFromItems("1232", 0, 1)
on ings.Id equals ingAllergens.IngredientId into ingAllergensData
from allergens in ingAllergensData.DefaultIfEmpty()
where ings.Table == "tblIng" || ings.Table == ""
select new {ings, allergens}).ToList();
linqpad 的响应:
编辑这是在 LINQPad 中生成的 SQL 查询:
-- Region Parameters
DECLARE @p0 Int = 1232
DECLARE @p1 Int = 0
DECLARE @p2 Int = 1232
DECLARE @p3 VarChar(1000) = '1232'
DECLARE @p4 SmallInt = 0
DECLARE @p5 Int = 1
DECLARE @p6 VarChar(1000) = 'tblIng'
DECLARE @p7 VarChar(1000) = ''
-- EndRegion
SELECT [t0].[prodId] AS [ProdId], [t0].[id] AS [Id], [t0].[parent] AS [Parent], [t0].[name] AS [Name], [t0].[ing_gtin] AS [Ing_gtin], [t0].[ing_artsup] AS [Ing_artsup], [t0].[table] AS [Table], [t0].[quantity] AS [Quantity], [t2].[test], [t2].[prodId] AS [ProdId2], [t2].[ingredientId] AS [IngredientId], [t2].[allergenId] AS [AllergenId], [t2].[allergenName] AS [AllergenName], [t2].[level_of_containment] AS [Level_of_containment]
FROM [dbo].[fnListIngredientsFromItem](@p0, @p1, @p2) AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[prodId], [t1].[ingredientId], [t1].[allergenId], [t1].[allergenName], [t1].[level_of_containment]
FROM [dbo].[fnListAllergensFromItems](@p3, @p4, @p5) AS [t1]
) AS [t2] ON [t0].[id] = ([t2].[ingredientId])
WHERE ([t0].[table] = @p6) OR ([t0].[table] = @p7)
我还尝试将相同的数字硬编码到 C# 中,但再次遇到相同的错误。
最佳答案
问题是 Entity Framework 需要知道 TVF 结果的主键列是什么才能进行左连接,而默认生成的 EDMX 文件不包含该信息。您可以通过将 TVF 结果映射到实体(而不是默认映射到复杂类型)来添加键值信息。
同一查询在 LINQPad 中起作用的原因是 LINQPad 中用于连接到数据库的默认数据上下文驱动程序使用 LINQ to SQL(而非 Entity Framework )。但我能够让查询在 Entity Framework 中运行(最终)。
我建立了一个类似于表值函数的本地 SQL Server 数据库:
CREATE FUNCTION fnListIngredientsFromItem(@prodId int, @itemType1 smallint, @parent int)
RETURNS TABLE
AS
RETURN (
select prodId = 1232, id = 1827, parent = 1232, name = 'Ossenhaaspunten', ing_gtin = 3003210089821, ing_artsup=141020, [table] = 'tblIng', quantity = '2 K'
);
go
CREATE FUNCTION fnListAllergensFromItems(@prodIdString varchar(1000), @itemType2 smallint, @lang int)
RETURNS TABLE
AS
RETURN (
select prodId = '1232', ingredientId = 1827, allergenId = 11, allergenName = 'fish', level_of_containment = 2
union all
select prodId = '1232', ingredientId = 1827, allergenId = 16, allergenName = 'tree nuts', level_of_containment = 2
union all
select prodId = '1232', ingredientId = 1827, allergenId = 12, allergenName = 'crustacean and shellfish', level_of_containment = 2
);
go
我使用 Entity Framework 6.1.2 创建了一个测试项目,并使用 Visual Studio 2013 中的实体数据模型设计器从数据库生成了一个 EDMX 文件。使用此设置,我在尝试运行时遇到了同样的错误该查询:
System.NotSupportedException
HResult=-2146233067
Message=The query attempted to call 'OuterApply' over a nested query, but 'OuterApply' did not have the appropriate keys.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.ApplyOpJoinOp(Op op, Node n)
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.VisitApplyOp(ApplyBaseOp op, Node n)
at System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitorOfT`1.Visit(OuterApplyOp op, Node n)
...
为左连接运行替代表达式会导致稍微不同的错误:
var ingredientAllergenData = (db.fnListIngredientsFromItem(1323, (short)0, 1)
.GroupJoin(db.fnListAllergensFromItems("1232", 0, 1),
ing => ing.id,
allergen => allergen.ingredientId,
(ing, allergen) => new { ing, allergen }
)
).ToList();
这是新异常的截断堆栈跟踪:
System.NotSupportedException
HResult=-2146233067
Message=The nested query does not have the appropriate keys.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.ConvertToSingleStreamNest(Node nestNode, Dictionary`2 varRefReplacementMap, VarList flattenedOutputVarList, SimpleColumnMap[]& parentKeyColumnMaps)
at System.Data.Entity.Core.Query.PlanCompiler.NestPullup.Visit(PhysicalProjectOp op, Node n)
at System.Data.Entity.Core.Query.InternalTrees.PhysicalProjectOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n)
...
Entity Framework是开源的,所以我们其实可以看看这个异常抛出的源码。此片段中的注释解释了问题所在 ( https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Core/Query/PlanCompiler/NestPullup.cs ):
// Make sure that the driving node has keys defined. Otherwise we're in
// trouble; we must be able to infer keys from the driving node.
var drivingNode = nestNode.Child0;
var drivingNodeKeys = Command.PullupKeys(drivingNode);
if (drivingNodeKeys.NoKeys)
{
// ALMINEEV: In this case we used to wrap drivingNode into a projection that would also project Edm.NewGuid() thus giving us a synthetic key.
// This solution did not work however due to a bug in SQL Server that allowed pulling non-deterministic functions above joins and applies, thus
// producing incorrect results. SQL Server bug was filed in "sqlbuvsts01\Sql Server" database as #725272.
// The only known path how we can get a keyless drivingNode is if
// - drivingNode is over a TVF call
// - TVF is declared as Collection(Row) is SSDL (the only form of TVF definitions at the moment)
// - TVF is not mapped to entities
// Note that if TVF is mapped to entities via function import mapping, and the user query is actually the call of the
// function import, we infer keys for the TVF from the c-space entity keys and their mappings.
throw new NotSupportedException(Strings.ADP_KeysRequiredForNesting);
}
这解释了导致该错误的路径,因此我们可以采取任何措施来摆脱该路径,从而解决问题。假设我们必须对表值函数的结果进行左连接,一种选择(也许是唯一的选择?)是将 TVF 的结果映射到具有主键的实体。然后 Entity Framework 将根据到该实体的映射知道 TVF 结果的键值,我们应该避免这些与丢失键相关的错误。
默认情况下,从数据库生成 EDMX 文件时,TVF 会映射到复杂类型。在 https://msdn.microsoft.com/en-us/library/vstudio/ee534438%28v=vs.100%29.aspx 上有关于如何更改它的说明。 .
在我的测试项目中,我添加了一个空表,其中的模式与 TVF 的输出相匹配,让模型设计器生成实体,然后我转到模型浏览器并更新函数导入以返回这些实体的集合实体(而不是自动生成的复杂类型)。进行这些更改后,相同的 LINQ 查询运行没有错误。
var ingredientAllergenData = (from ings in db.fnListIngredientsFromItem(productId, (short)itemType, productId)
join ingAllergens in db.fnListAllergensFromItems(productId.ToString(CultureInfo.InvariantCulture), (short)itemType, currentLang)
on ings.id equals ingAllergens.ingredientId into ingAllergensData
from allergens in ingAllergensData.DefaultIfEmpty()
where ings.table == "tblIng" || ings.table == ""
select new {ings, allergens}).ToList();
这是查询给我的跟踪 SQL:
SELECT
1 AS [C1],
[Extent1].[prodId] AS [prodId],
[Extent1].[id] AS [id],
[Extent1].[parent] AS [parent],
[Extent1].[name] AS [name],
[Extent1].[ing_gtin] AS [ing_gtin],
[Extent1].[ing_artsup] AS [ing_artsup],
[Extent1].[table] AS [table],
[Extent1].[quantity] AS [quantity],
[Extent2].[prodId] AS [prodId1],
[Extent2].[ingredientId] AS [ingredientId],
[Extent2].[allergenId] AS [allergenId],
[Extent2].[allergenName] AS [allergenName],
[Extent2].[level_of_containment] AS [level_of_containment]
FROM [dbo].[fnListIngredientsFromItem](@prodId, @itemType1, @parent) AS [Extent1]
LEFT OUTER JOIN [dbo].[fnListAllergensFromItems](@prodIdString, @itemType2, @lang) AS [Extent2] ON ([Extent1].[id] = [Extent2].[ingredientId]) OR (([Extent1].[id] IS NULL) AND ([Extent2].[ingredientId] IS NULL))
WHERE [Extent1].[table] IN ('tblIng','')
关于c# - LINQ左外连接查询错误: OuterApply did not have the appropriate keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27255181/
我有一个数据集,其列如下所示: Consumer ID | Product ID | Time Period | Product Score 1 | 1 | 1
举个例子。有一个我想使用的对象,称它为 Doodad。 Doodad 元素对浏览器事件的处理很差。 Doodad 的典型实例是 Doodad someDoodad = new Doodad();。显然
我正在构建一个基于 Java 的 Web 应用程序(主要是使用 Tomcat 部署的 JSP)。用户数量永远不会超过 30 人。它是一个工作日志,因此用户将不断更新/访问数据库 (SQL Server
我有一个名为 account 的抽象类,如下所示 - abstract class Account { private int number; private String owner
我目前忙于响应式字体大小。我很清楚 vh、vw、vmin 等单位的含义。 但我无法与他们一起完成令人信服的事情。它们要么在大屏幕上变大,要么在小屏幕上变小。反之亦然。 是否有任何适用于哪些值的一般规则
我定义了一个名为“FilterCriteria”的类,它有一堆与之关联的函数 .m 文件(getAMask、getBMask 等)。当我创建 FilterCriteria 对象并使用它调用函数时,我没
我正在尝试保存具有“类似论坛”结构的数据: 这是简化的数据模型: +---------------+ | Forum | | | | Name
我正在寻找一种在 Java 中验证数据库模式是否正常、默认值是否正常、触发器是否正常的方法。我找到了很多框架来测试数据库交互,但找不到可以让我测试表和模式的东西。有什么框架吗?如果数据库尚未同步,它将
题目地址:https://leetcode.com/problems/friends-of-appropriate-ages/description/ 题目描述: Some people will
我有以下代码: fn main() { let get = |v: &u32| -> &u32 { v }; let x : u32 = 0; let
在下面的代码中选择适当的 Web 服务方法的逻辑是什么? 客户: HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic
背景:我正在创建一个返回切片引用的迭代器 &[T] ,但数据向量需要保持不变。迭代器不能修改原始数据,但修改后必须重复返回同一个切片指针。我考虑过让我的迭代器拥有一个 Vec ,但我想避免这种情况(而
我有一个接受枚举引用的函数,我需要通过匹配枚举并读取其内容来解析它。枚举的一种变体(不在下面的简化的最小工作示例中)可能包含枚举本身的类型作为值,因此我可能需要递归调用相同的函数来解析它的值。 我想编
DROP TABLE temp; CREATE TABLE `temp` ( `CallID` bigint(8) unsigned NOT NULL,
无法编译以下内容。这里有什么问题吗? class B; class A { public: void DoSomething() { ... B* my
我在 Wheel of Luck Game 中需要其他帮助。我需要在 6 个 div 中选择 3 个 DIV。但是如果我选择如下图所示的 div 会发生什么,它将分别选择 6 No DIV 而不是 5
我想在不使用 STL 的情况下创建一个数组链表。但是,我在将数组传递到我的链接列表时遇到困难... 编译时出现上面列出的错误。我需要如何将数组传递给链表?谢谢! (有问题的代码有**标记,如果测试请去
我正在使用 Entity Framework 作为我的 ORM 对两个 SQL 函数进行连接。执行查询时,我收到此错误消息: The query attempted to call 'Outer Ap
我所做的只是导入一个 npm 包 import TimePicker from 'simple-timepicker-react' render () { return (
我有一个使用 bower 和 webpack 的 React 项目。 我正在尝试使用这个模块 https://github.com/jrowny/react-absolute-grid . 我用 np
我是一名优秀的程序员,十分优秀!