Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Exposing a Stored Procedure in WCF Data Service

DZone's Guide to

Exposing a Stored Procedure in WCF Data Service

·
Free Resource

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 a the following stored procedure:

CREATE PROCEDURE dbo.GetCoursesOrderByTitle
AS
BEGIN
SET NOCOUNT ON
SELECT CourseID, Title, Days, [Time], Location, Credits, DepartmentID
FROM Course
ORDER BY Title ASC
END

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 map the stored procedure as a FunctionImport in the model. If you want to understand how to create this sort of mapping go to a 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 lets 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:

Lets 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;
}

[WebGet]
public IQueryable<Course> GetCoursesOrderByTitle()
{
return CurrentDataSource
.GetCoursesOrderByTitle()
.AsQueryable();
}
}

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:

http://localhost:43054/SchoolDataService.svc/GetCoursesOrderByTitle

And the result will be:
Runing Service Operation

Summary

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.

Topics:

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}