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.

There are three conditions while using LINQ to SQL class with WCF Data Service. One of below condition must be true

1. The Primary Key column of the table must have name ID.

2. Primary key column of the table must have ID appended to that. For example 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 below steps

Step1

Launch visual studio and create a web application

Step2

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

clip_image002

b. Click on .cs file

clip_image003

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

d. Locate the entity class and put the DataServiceKey attribute

clip_image005

Above SpeakerId is 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

clip_image007

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

clip_image009

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com