Click here to Skip to main content
Licence CPOL
First Posted 26 Jul 2010
Views 12,268
Bookmarked 11 times

Defining Custom Functions in Entity Framework

By | 26 Jul 2010 | Article
The article explains how to define custom functions in Entity Framework
 
Part of The SQL Zone sponsored by
See Also

Introduction

During the SDP conference, I have been asked about the use of functions inside the Entity Data Model.
This article will try to answer one such question of how to define a custom function inside EF.

Custom Functions in Entity Framework

One of the capabilities of EF since EF1 was the creation of custom functions inside the SSDL part of the model. After their creation, we could consume them like other imported functions. In that way, we could define functions in the model which were acting like stored procedures but without being a part of the database. This, of course, means that in order to use this feature, we needed to write some XML code inside the SSDL.

Defining a Custom Function

The first thing to do is to open the model in XML editor and to add a Function element inside the Schema element in the SSDL part of the XML. Inside the Function element, we add a CommandText element which will hold our T-SQL expression. We can also add to the function parameters.

The following XML fragment shows an example of a custom function:

<Function Name="GetCoursesByCredit" IsComposable="false">
  <CommandText>
    SELECT *
    FROM Course
    WHERE Credits = @Credits
  </CommandText>  
  <Parameter Name="Credits" Type="int" Mode="In">
  </Parameter>
</Function>

After the creation of that function, we can use the Add Function Import wizard to import that function to the ObjectContext:

FunctionImport Wizard

and then to run a test like this one to see the function in action:

using (var context = new SchoolEntities())
{
    var query = context.GetCoursesByCredit(3);
    foreach (var course in query)
    {
        Console.WriteLine(course.Title);
    }
    Console.ReadLine();
}

Summary

EF enables us to add custom SSDL functions to our model. These functions can be handled like other imported functions (UDFs or stored procedures). This feature should be used rarely in my opinion and only when there is a T-SQL functionality that we must have but EF doesn’t support.

History

  • 26th July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gil Fink

Architect
Sela Group
Israel Israel

Member

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat article PinmemberMichał Zalewski22:08 2 Aug '10  
GeneralRe: Great article PinmemberGil Fink1:52 3 Aug '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid