Entity SQL: Canonical Functions

Canonical functions were introduced in the Beta 2 release of Entity Framework. Their purpose is to expose a [virtually] canonical API beyond the core language constructs. Consumers of canonical functions should be aware that Entity Framework defines only the “syntax” part of the API. The behavior is entirely up to the store provider. Not the store, but the ADO.NET provider used to connect to it. That means different providers for the same store may map functions differently. A decent store provider would document how exactly each canonical function is mapped to a store function. For SqlClient that reference is placed under:

ADO.NET Entity Framework > Feature Reference > .NET Framework Data Provider for SQL Server

in the Entity Framework documentation.

 

Canonical functions are contained in the Edm namespace. Specifying the Edm namespace is optional. However, specifying a store namespace is not. For instance, all three function calls in the following Entity SQL query do exactly the same:

 

ROW(Edm.Length('abc') AS [Edm.Length],

    Length('abc') AS [Length],

    SqlServer.LEN('abc') AS [SqlServer.Len])

-- Powered by eSqlBlast

 

Edm.Length

Length

SqlServer.Len

3

3

3

 

The T-SQL that SqlClient produces is:

SELECT

    1 AS [C1],

    CAST(LEN('abc') AS int) AS [C2],

    CAST(LEN('abc') AS int) AS [C3],

    CAST(LEN('abc') AS int) AS [C4]

FROM  ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]

 

Canonical functions are the preferred way to access functionality outside the core language, because they keep the queries [virtually] portable. The set of canonical functions defined by Entity Framework for version 1 is a tiny subset of the set of functions any major database server exposes, but it will grow over time.