With the release of EF5, a new support for database (SQL Server) UDF is available, depending on the following connection: Https://msdn.microsoft.com/en-us/data/hh859577.aspx, This feature is also inherited by the newly released EF6. The problem is that this UDF seems to be just for tvf-table Value function, but unfortunately, it cannot be imported when trying to import a scalar function.
The reason is that there is no support for scalar function in the EF6 version ...
But there are also workaround, because EF provides the Dbfunction class, which allows us to write a Cutsom method directly in DbContext and then map to a section of auto-generated XML code in SSDL, which has EF Model Auto-generated, a complete example:
Scalar Function:
CREATE FUNCTION [dbo]. [Function20150410] (@param1 int, @param2 int) RETURNS Intasbeginreturn @param1 + @param2END
XML Code:
<function name= "Function20150410" aggregate= "false" builtin= "false" niladicfunction= "false" iscomposable= "true" parametertypesemantics= "allowimplicitconversion" schema= "dbo" returntype= "int" > <parameter Name= "param1 "Type=" int "mode=" in "/> <parameter name=" param2 "type=" int "mode=" in "/> </Function>
Custom Method in DbContext Class:
[Dbfunction ("Dfdbmodel.store", "Function20150410")] Public objectresult<int> getcontentbyidandcul (int id, int culture) { var objectContext = ( Iobjectcontextadapter). ObjectContext; var parameters = new list<objectparameter> (); Parameters. ADD (New ObjectParameter ("id", id)); Parameters. ADD (New ObjectParameter ("culture", Culture)); Return objectcontext.createquery<int> ("DFDBModel.Store.Function20150410 (@Id, @Culture)", parameters. ToArray ()). Execute (mergeoption.notracking); }
Call the This method:
using (dfdbentities db=new dfdbentities ()) { var result = db. Getcontentbyidandcul (1, 1). FirstOrDefault (); }
Entity Framework 6 handles user Defined Function (UDF SQL Server)