OData Programming Cookbook for .NET Developers
上QQ阅读APP看书,第一时间看更新

Building an OData service with WCF Data Service and LINQ to SQL

In addition to ADO.NET Entity Framework, LINQ to SQL is another popular and powerful component we can use for mapping relational database objects to .NET CLR class objects. Many popular RDBMS (such as SQL Server and Oracle) have provided LINQ to SQL providers. And for WCF Data Service, it is quite reasonable to add support for exposing a LINQ to SQL based data source via OData service endpoints. In this recipe, we will introduce you to creating an OData service from a LINQ to SQL based data source model.

Getting ready

Make sure you have a sample database, such as Northwind, installed in a local SQL Server instance. You can use an SQL Express instance (the free version of SQL Server) for convenience.

The source code for this recipe can be found in the \ch01\ODataLINQ2SQLServiceSln\ directory.

How to do it...

You can follow the steps given for creating an OData service from LINQ to SQL data entities:

  1. Create a new ASP.NET Empty Web Application in Visual Studio 2010.
  2. Create the LINQ to SQL data model types by using the LINQ to SQL Classes item template (see the following screenshot).
    How to do it...

    After the data model is created, we can use Visual Studio Server Explorer to drag certain tables (from the sample database) into the data model designer. This will make the Visual Studio IDE create the corresponding data entity types.

    Tip

    Save all items in the project so as to make sure Visual Studio IDE has compiled the generated LINQ to SQL data model types.

  3. Create a new WCF Data Service based on the generated LINQ to SQL data model.

    This time, we use the LINQ to SQL data model class as the generic parameter of the service class (see the following code snippet).

    public class NWODataService : DataService< ODataLINQ2SQLService.NorthwindDataContext >
    {
    // This method is called only once to initialize service-wide // policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    }
    }
    
  4. Select the .svc service file in Visual Studio and launch it through the View in browser context menu.

How it works...

Although we directly use the LINQ to SQL data model class as the data source, the WCF Data Service runtime actually treats the LINQ to SQL data model class like a custom data source type. Therefore, any public member (of the data model class) who implements the IQueryable interface will be exposed as an entity set in the generated service. We will talk more about using custom data source type for WCF Data Service within the Using custom data objects as the data source of WCF Data Service recipe of this chapter.

There's more...

By default, the WCF Data Service, which uses the LINQ to SQL data model class, does not support editing/updating operations. In order to make the LINQ to SQL based WCF Data Service support editing/updating, we need to implement the IUpdatable interface (under System.Data.Services namespace) on the LINQ to SQL data model class (see the following code snippet).

partial class NorthwindDataContext: IUpdatable
{
......
}

For detailed information about implementing IUpdatable interface for LINQ to SQL data model class, you can refer to the following MSDN reference:

How to: Create a Data Service Using a LINQ to SQL Data Source (WCF Data Services) available at http://msdn.microsoft.com/en-us/library/ee373841.aspx

See also

  • Building an OData service via WCF Data Service and ADO.NET Entity Framework recipe
  • Using custom data objects as the data source of WCF Data Service recipe