Entity Framework 6 handles user Defined Function (UDF SQL Server)

Source: Internet
Author: User
Tags scalar

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.