Today the Entity Framework, and more specifically the Entity Data Model, have a limited notion of Functions.
We are currently restricted to Function Imports that allow stored procedures to be invoked, and Canonical / Store Functions for database independent and database specific functions respectively.
Now however we want to support functions defined, not just declared, in the EDM (aka. the CSDL).
An example would be:
<Function Name="GetAge" ReturnType="Edm.Int32"> <Parameter Name="Person" Type="Model.Person" /> <DefiningExpression> Edm.DiffYears(Edm.CurrentDateTime(), Person.Birthday) </DefiningExpression> </Function>
Here are some things to notice:
It is trivial to use the function via eSQL. For example:
SELECT Namespace.GetAge(p) FROM Container.People AS P WHERE P.Firstname = ‘Jim’
Here we get Jim's age, assuming of course there is only one Jim!
It is also possible to compose functions together, you must simply ensure return types and target parameter types are the same (in the case of named types) or structurally equivalent (in the case of row types or collections of row types).
Things get a little trickier when you are dealing with functions that return sets, for example imagine a function that returns someone's friends, used in conjunction with the GetAge function:
SELECT VALUE (F) FROM Container.People AS P CROSS APPLY Namespace.GetFriends(P) AS F WHERE Namespace.GetAge(P) > 21
Here we get all the friends of people older than 21.
As you can see to do this sort of thing you need a crash course in eSQL.
It is also possible to use these functions in LINQ, but this does require a extra step to create an appropriate stub function in the CLR.
This solution is based on the techniques described here, and involves creating a Stub function in the CLR language of your choice, and annotating it something like this:
[EdmFunction("Namespace", "GetAge")] public static int GetAge(Person p) { throw new NotSupportedException(…); }
The Entity Framework uses the signature of the function and the EdmFunction attribute to map calls to this function when encountered to the appropriate Model Defined Function.
Once you have this stub it is then trivial to use it in a LINQ query like this:
var peopleOver21 = from p in ctx.People where GetAge(p) < 21 select p;
Indeed if you are familiar with LINQ you will probably find composing functions together a lot easier too:
However the existence of the stub allows you to create LINQ expressions that compile correctly, and then at runtime, when used in a LINQ to Entities query, the function call is simply translated by the entity framework into a query that runs in the database.
As you can see Model Defined Function's are very powerful, and this post has barely scratched the surface of possibilities they open up.
The Entity Framework team would love to hear your comments.
Alex James Program Manager, Entity Framework Team
This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at this post.