Along with Stored Procedures, you can bring UDFs into an EF Entity Data Model which also surfaces as a function in SSDL.
Unlike Stored Procedures, however, a UDF is composable on the server side and this is reflected in its attributes.
<Function Name="ufnLBtoKG" ReturnType="nvarchar" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo"> <Parameter Name="Pounds" Type="int" Mode="In" /> </Function>
But the differences don't end there.
Implementing a UDF and querying with a UDF is not documented anywhere that I can find except that I finally found a hint buried in an MSDN forum thread.
The compiler also gave me a few hints when I was doing things incorrectly.
1) UDFs do not get mapped back to the CSDL via function mapping. This is because of their composability. You need to call them directly from SSDL which is unlike any other querying in Entity Framework.
2) Like many of the mapped functions that come from stored procedures, you can only call these from Entity SQL.
Mapped functions that return an entity can be called from the objectcontext. Everything else must be called via Entity SQL.
Here is an Entity SQL expression that uses the ufnLBtoKG function.
SELECT c.LastName, c.WeightInPounds, MyModel.Store.ufnLBtoKG(c.WeightInPounds)
FROM MyEntities.Contacts AS c
The function is called using its strongly typed name but from the store layer, not the conceptual layer.
Since it took me a while to figure this out, I thought I would save someone that aggravation.
If you follow what feels like the normal steps and map the ufnLBtoKG function back to the CSDL, when EF attempts to execute a query that uses it you get this exception message:
A FunctionImport is mapped to a storage function 'MyModel.Store.ufnLBtoKG' that can be composed.
Only stored procedure functions may be mapped.
That's pretty clear although it confused me at first because I didn't understand how to call the function yet but I finally got it worked out.