Click here to Skip to main content
13,250,043 members (67,121 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 25 Oct 2010

Exposing a Stored Procedure in WCF Data Service

, 25 Oct 2010
Rate this:
Please Sign up or sign in to vote.
How to expose a stored procedure in WCF data service

Today, I answered a question in the data platform development forums. The question was simple – how to expose a stored procedure which is mapped to an Entity Framework model through a WCF Data Service. This post will show you exactly how to do this.

The Stored Procedure

First I’ve created the following stored procedure:

CREATE PROCEDURE dbo.GetCoursesOrderByTitle


    SELECT CourseID, Title, Days, [Time], Location, Credits, DepartmentID
    FROM Course

    ORDER BY Title ASC

The procedure is simple and returns all the courses ordered by title. Of course, this could be achieved by the use of LINQ to Data Services but I wanted to show the concept of exposing SP in your WCF Data Service.

The Model

After creating the stored procedure I’ve created the Entity Framework model which for simplicity holds only a course entity and also maps the stored procedure as a FunctionImport in the model. If you want to understand how to create this sort of mapping, go to an old post I wrote about it. The model will look like:

Entity Designer Diagram   Model Browser

Creating the WCF Data Service

After we have our model, let's create the WCF Data Service. In order to expose the stored procedure, we will have to create a service operation method. Service operations are endpoints that you can create in WCF Data Services in order to add business logic that can’t be supported by the service RESTful interface. These endpoints are consumed like any other endpoints in the REST interface that the WCF Data Service exposes. For further reading about Service Operations, I recommend these two posts:

Let's look at the service:

public class SchoolDataService : DataService<SchoolEntities>
  public static void InitializeService(DataServiceConfiguration config)
    config.SetEntitySetAccessRule("Courses", EntitySetRights.AllRead);
    config.SetServiceOperationAccessRule("GetCoursesOrderByTitle", ServiceOperationRights.AllRead);

    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  public IQueryable<Course> GetCoursesOrderByTitle()
    return CurrentDataSource


As you can see, I use the CurrentDataSource in order to get the Entity Framework ObjectContext and then to execute the stored procedure using its FunctionImport. I also need to set the service operation access rule in order to expose the operation. If I want to consume the service operation right now and not from a client, I can view the service in browser and then create the following link for example in order to call the operation:


And the result will be:

Runing Service Operation


Exposing a stored procedure through WCF Data Services can be achieved by providing service endpoints using the service operation feature. In the post, I showed how you can do that by using Entity Framework.


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


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...

Comments and Discussions

QuestionHow to consume the result in PHP Pin
Member 79208638-Aug-12 8:59
memberMember 79208638-Aug-12 8:59 
QuestionParameters Pin
Obeleh5-Jul-12 5:42
memberObeleh5-Jul-12 5:42 
AnswerRe: Parameters Pin
Obeleh5-Jul-12 6:08
memberObeleh5-Jul-12 6:08 
GeneralMy vote of 2 Pin
snehaljogiya2-May-12 18:09
membersnehaljogiya2-May-12 18:09 
GeneralRe: My vote of 2 Pin
Rajinder Singh17-Jan-13 10:09
memberRajinder Singh17-Jan-13 10:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 25 Oct 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid