How to: Create a Data Service Using a LINQ to SQL Data Source (WCF Data Services)
WCF Data Services exposes entity data as a data service. The reflection provider enables you to define a data model that is based on any class that exposes members that return an IQueryable<T> implementation. To be able to make updates to data in the data source, these classes must also implement the IUpdatable interface. For more information, see Data Services Providers. This topic shows you how to create LINQ to SQL classes that access the Northwind sample database by using the reflection provider, as well as how to create the data service that is based on these data classes.
From within a Visual Basic or C# application, on the Project menu, click Add New Item.
Click the LINQ to SQL Classes template.
Change the name to Northwind.dbml.
The Northwind.dbml file is added to the project and the Object Relational Designer (O/R Designer) opens.
In Server/Database Explorer, under Northwind, expand Tables and drag the
Customerstable onto the Object Relational Designer (O/R Designer).
Customerentity class is created and appears on the design surface.
Repeat step 6 for the
Right-click the new .dbml file that represents the LINQ to SQL classes and click View Code.
This creates a new code-behind page named Northwind.cs that contains a partial class definition for the class that inherits from the DataContext class, which in this case is
Replace the contents of the Northwind.cs code file with the following code. This code implements the reflection provider by extending the DataContext and data classes generated by LINQ to SQL:
[!CODE [Astoria Linq Provider#Linq2SqlProvider](../CodeSnippet/VS_Snippets_Misc/astoria linq provider#linq2sqlprovider)]
In Solution Explorer, right-click the name of your ASP.NET project, and then click Add New Item.
In the Add New Item dialog box, select WCF Data Service.
Supply a name for the service, and then click OK.
Visual Studio creates the XML markup and code files for the new service. By default, the code-editor window opens.
In the code for the data service, replace the comment
/* TODO: put your data source class name here */in the definition of the class that defines the data service with the type that is the entity container of the data model, which in this case is
In the code for the data service, replace the placeholder code in the
InitializeServicefunction with the following:
[!CODE [Astoria Linq Provider#EnableAccess](../CodeSnippet/VS_Snippets_Misc/astoria linq provider#enableaccess)]
This enables authorized clients to access resources for the three specified entity sets.
To test the Northwind.svc data service by using a Web browser, follow the instructions in the topic Accessing the Service from a Web Browser.