Calling a User-Defined Function from the Entity Framework

05 Jan

Recently I have been working heavily in SQL server writing SPROCS but using a few User defined functions just to make things easier.  Well this morning I was back knee deep in .NET code and thought wouldn’t it be nice if I could use the Entity Framework to call out to my helper UDFs.

Well the answer is that its possible, infact its actually very simple, the only problem I found was that there were not many easy to understand articles on how to accompish this on the net.

So here is a quick Post on how to call User Defined Functions and return a Scalar Value from the Entity Framework.

Firstly we need to add the function to the Model Store.  Do to this open the Model Browser Window, Right-Click the Stored Procedures Folder and Select ‘Update Model from Database’

Add a User Defined Function to the Store


Once the function has been added, you can only call the function via SQL directly so in your code write some thing similar to this:

Calling the UDF from code

And thats all there is to it.  Told you it was easy!!


3 responses to “Calling a User-Defined Function from the Entity Framework

  1. jrichview

    January 24, 2012 at 3:25 pm

    How about table-valued functions? They don’t seem to show up in the list of “stored procedures”

  2. shaharyar

    January 3, 2013 at 9:18 am


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: