Entity framework 实体框架5:如何外部连接表值函数

Entity framework 实体框架5:如何外部连接表值函数,entity-framework,entity-framework-5,Entity Framework,Entity Framework 5,我试图在LINQ查询中使用内联表值函数对外联接表,但在运行时出现查询编译错误: “查询试图通过嵌套查询调用'outerappy',但'outerappy'没有相应的键。” 我的linq声明如下所示: var testQuery = (from accountBase in ViewContext.AccountBases join advisorConcatRaw in ViewContext.UFN_AccountAdvisorsC

我试图在LINQ查询中使用内联表值函数对外联接表,但在运行时出现查询编译错误:

“查询试图通过嵌套查询调用'outerappy',但'outerappy'没有相应的键。”

我的linq声明如下所示:

     var testQuery = (from accountBase in ViewContext.AccountBases

                         join advisorConcatRaw in ViewContext.UFN_AccountAdvisorsConcatenated_Get()
                                on accountBase.AccountId equals advisorConcatRaw.AccountId into advisorConcatOuter
                         from advisorConcat in advisorConcatOuter.DefaultIfEmpty()

                         select new
                         {
                             accountBase.AccountId,
                             advisorConcat.Advisors
                         }).ToList();
函数定义如下:

CREATE FUNCTION dbo.UFN_AccountAdvisorsConcatenated_Get()
RETURNS TABLE
AS
RETURN
    SELECT  AP.AccountId,
            LEFT(AP.Advisors, LEN(AP.Advisors) - 1) AS Advisors
      FROM  (   SELECT  DISTINCT 
                        AP.AccountId,
                        (   SELECT  AP2.PropertyValue + ', '
                            FROM    dbo.AccountProperty AP2 WITH (NOLOCK)
                            WHERE   AP2.AccountId = AP.AccountId
                            AND     AP2.AccountPropertyTypeId = 1 -- Advisor 
                            FOR XML PATH('')) AS Advisors
                FROM    dbo.AccountProperty AP WITH (NOLOCK)) AP;
 SELECT ab.accountid,
       advisorConcat.Advisors
FROM   accountbase ab
       LEFT OUTER JOIN dbo.Ufn_accountadvisorsconcatenated_get() advisorConcat
                    ON ab.accountid = advisorConcat.accountid 
我可以直接在sql中成功执行连接,如下所示:

CREATE FUNCTION dbo.UFN_AccountAdvisorsConcatenated_Get()
RETURNS TABLE
AS
RETURN
    SELECT  AP.AccountId,
            LEFT(AP.Advisors, LEN(AP.Advisors) - 1) AS Advisors
      FROM  (   SELECT  DISTINCT 
                        AP.AccountId,
                        (   SELECT  AP2.PropertyValue + ', '
                            FROM    dbo.AccountProperty AP2 WITH (NOLOCK)
                            WHERE   AP2.AccountId = AP.AccountId
                            AND     AP2.AccountPropertyTypeId = 1 -- Advisor 
                            FOR XML PATH('')) AS Advisors
                FROM    dbo.AccountProperty AP WITH (NOLOCK)) AP;
 SELECT ab.accountid,
       advisorConcat.Advisors
FROM   accountbase ab
       LEFT OUTER JOIN dbo.Ufn_accountadvisorsconcatenated_get() advisorConcat
                    ON ab.accountid = advisorConcat.accountid 

是否有人有一个在LINQtoEntities中将内联TVF连接到表的工作示例?或者这是一个已知的缺陷,等等?非常感谢。

实体框架需要知道TVF结果的主键列是什么来执行左连接。基本上,您需要创建一个与TVF结果具有相同模式的假表,并在模型浏览器中更新TVF以返回新创建的表类型,而不是默认的复杂类型。您可以参考以获取更多详细信息

有相同的问题,有解决方案吗?Microsoft对此仍然没有响应-此问题是否会在下一版本的EF中得到解决?对于EF6,请查看,对于EF Core,它现在内置了
DbFunctionAttribute