Introduction

WCF Data Services is a part of the .NET Framework that provides the ability to create Windows Communication Foundation (WCF) services that use the Atom Publishing protocol (AtomPub) and Open Data protocol (OData) to expose and consume data over the web.WCF data Services are a new feature in .net 4 framework.It exposes data by using the semantics of representational state transfer(REST). OData provides the ability to perform create, retrieve, update, and delete (CRUD) operations and can be used on a variety of sources such as relational databases, file systems,content management systems, and traditional websites.

WCF Data Services provides an easy mean of enabling clients to use backend functionalities on databases residing on the server side.It creates a cloud based interface service layer juxtaposed between the client and the Entity Data Model(EDM) which on the other hand interacts with the database.We can use LINQ to SQL or LINQ to Entities for the EDM.

Steps to create WCF Data Services

In this article we will look into the basic CRUD operation for Employee Entity by using WCF Data services in a step by step manner.We will make a simple Employee Database and will employe the needed operations based on the Employee model.

Then choose EntityDataModel template from the list of available templates.

Click on "Add" button.Choose "Generate from Database" as the model from the "Choose Model Contents" screen and click on the "Next Button".

The next screen that appears is the "Choose your Data Connection" where we need to enter the proper connection.Once done (i.e. the Test Connection succeeds), eneter "WCFDataServiceEntities" in the "Save Entity Connection Settings in App.Config as" textbox.Also choose "Yes, include sensitive data in the connection string" radiobutton.And then click on the "Next" button.

Now select the "WCFDataServiceSite" project.Right click ->Add New Item.Next choose the "WCF Data Service" template from the web node.Let us name it as "EmployeeDataService.svc" and click on "Add" button.

After this let us open the "EmployeeDataService.svc.cs" file and we will find that two "TODO" comments are generated to direct our attention to code that needs to be changed for the service to be operable

Put the "WCFDataServiceEntities" in the

DataService<WCFDataServiceEntities>

and put "*" in

config.SetEntitySetAccessRule(

"*", EntitySetRights.All);

The use of "*" character indicates that we want to include all the tables in the entity set."EntitySetRights.All" ensures authorization to create, read, update, and delete data.So the code will now look as under

using

System.Data.Services;

using

System.Data.Services.Common;

using

WCFDataServiceDal;

namespace

WCFDataServiceSite

{

publicclassEmployeeDataService : DataService<WCFDataServiceEntities>

{

// This method is called only once to initialize service-wide policies.

publicstaticvoid InitializeService(DataServiceConfiguration config)

{

// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

//Add the record to the employee collectionclient.AddToEmployees(emp);

//Save the changesclient.SaveChanges();

In this case we first find the "Employee" entity and assign the new values to it.Then we add it to the Employees collection by using the "AddToEmployees" method.And finally, we save the changes.Once the record has been inserted properly, the system will update the same with a positive response.

In this case whichever "Select" checkbox, the user will choose ,only those records will be deleted.Initially we are finding the record by the "EmpId" and then deleting that record and finally saving the changes

Conclusion

WCF Data Service enables clients to use a web application to access data residing on the server.We can create an EDM that resides between the Database and WCF DataService.The WCF DataService was hosted on the server but the processing was done at the client side.We can create a client application to access the service.This article,I believe, has given us substantial amount of knowledge to do so.Hope this will be helpful.Thanks for reading.Zip file is attached.

Have some query.
1. There is no Binding and Contract detail in WCF Data service, so will you explain how this WCF Data service is working here?
2. It seems that there is no DatContract and Operation contract, Is this all are handled by DataService class??