WCF Data Service with LINQ to SQL Class

We have seen many articles and blogs talking about using WCF Data Service with ADO.Net Entity model generated class as model. In this article we are going to see how to use WCF Data Service with LINQ to SQL Class.

To use LINQ to SQL class with WCF Data Service, one of the following three conditions must be true:

  1. The Primary Key column of the table must have name ID.
  2. The Primary key column of the table must have an ID appended to it. For example the name of the column should be in the format EmpID, ProductID , RollNoID etc.
  3. Third is, let us say you have to expose CRUD operation on your existing data base where you cannot change the existing columns. In that case follow the following steps

Step 1

Launch Visual Studio and create a web application

Step 2

a. Right click on the web application project and add data model using LINQ to SQL class.


b. Click on .cs file


c. Add the namespace System.Data.Services.Common in the class DataClasses1.designer.cs

d. Locate the entity class and put the DataServiceKey attribute


Above SpeakerId is the name of the primary key column in the table Blogger.

Step 3

a. Right click on the Web Application project and add a new item by selecting WCF Data Service from Web Tab


b. Now provide the data class context name as the model.


Now we are done and we can run the WCF Data Service.