官术网_书友最值得收藏!

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
主站蜘蛛池模板: 曲水县| 吴川市| 丰镇市| 新田县| 江口县| 奉节县| 荔波县| 淮滨县| 当涂县| 满城县| 灵台县| 安龙县| 闽清县| 阳原县| 双流县| 竹北市| 平江县| 三原县| 岱山县| 龙里县| 宜兰市| 莆田市| 西平县| 庆城县| 康保县| 凤山市| 深水埗区| 滨州市| 渝北区| 阳朔县| 淳化县| 霍林郭勒市| 句容市| 东平县| 辉县市| 图片| 佛教| 凌源市| 扶绥县| 孙吴县| 通道|