the problem is that entity framework needs to know what the primary key columns of the tvf results are to do a left join, and the default generated edmx file does not contain that information. you can add the key value information by mapping the tvf results to an entity (instead of the default of mapping to a complex type).

the reason the same query works in linqpad is that the default data context driver for connecting to a database in linqpad uses linq to sql (not entity framework). but i was able to get the query to run in entity framework (eventually).

i set up a local sql server database similar table-valued functions:

create function fnlistingredientsfromitem(@prodid int, @itemtype1 smallint, @parent int)
returns table 
return (
    select prodid = 1232, id = 1827, parent = 1232, name = 'ossenhaaspunten', ing_gtin = 3003210089821, ing_artsup=141020, [table] = 'tbling', quantity = '2 k'
create function fnlistallergensfromitems(@prodidstring varchar(1000), @itemtype2 smallint, @lang int)
returns table 
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

and i created a test project using entity framework 6.1.2 and generated an edmx file from the database using the entity data model designer in visual studio 2013. with this setup, i was able to get the same error when trying to run that query:

    message=the query attempted to call 'outerapply' over a nested query, but 'outerapply' did not have the appropriate keys.
        at op, node n)
        at op, node n)
        at`1.visit(outerapplyop op, node n)

running an alternate expression for a left join resulted in a slightly different error:

var ingredientallergendata = (db.fnlistingredientsfromitem(1323, (short)0, 1)
    .groupjoin(db.fnlistallergensfromitems("1232", 0, 1),
        ing =>,
        allergen => allergen.ingredientid,
        (ing, allergen) => new { ing, allergen }

here is a truncated stacktrace from the new exception:

    message=the nested query does not have the appropriate keys.
        at nestnode, dictionary`2 varrefreplacementmap, varlist flattenedoutputvarlist, simplecolumnmap[]& parentkeycolumnmaps)
        at op, node n)
        at[tresulttype](basicopvisitoroft`1 v, node n)

entity framework is open source, so we can actually look at the source code where this exception is thrown. the comments in this snippet explains what the problem is (

// 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);

that explains the path that leads to that error, so anything we can do to get off that path should fix the problem. assuming we have to do that left join on the results of a table-valued function, one option (maybe the only option?) is to map the results of the tvf to an entity that has a primary key. then entity framework will know the key values of the tvf results based on the mapping to that entity, and we should avoid these errors related to missing keys.

by default when generating an edmx file from the database, a tvf is mapped to a complex type. there are instructions for how to change it at

in my test project, i added an empty table with a schema that matched the output of the tvfs to get the model designer to generate entities, then i went to the model browser and updated the function imports to return a collection of these entities (instead of the auto-generated complex types). after making these changes, that same linq query ran without errors.

var ingredientallergendata = (from ings in db.fnlistingredientsfromitem(productid, (short)itemtype, productid)
                             join ingallergens in db.fnlistallergensfromitems(productid.tostring(cultureinfo.invariantculture), (short)itemtype, currentlang)
                             on equals ingallergens.ingredientid into ingallergensdata
                             from allergens in ingallergensdata.defaultifempty()
                             where ings.table == "tbling" || ings.table == ""
                             select new {ings, allergens}).tolist();

here is the trace sql that the query gave me:

    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','')

Related Query

More Query from same tag