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

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
主站蜘蛛池模板: 临西县| 新建县| 武川县| 彩票| 汝州市| 凤山县| 遂宁市| 长治市| 安丘市| 浏阳市| 华阴市| 任丘市| 新野县| 梅州市| 壤塘县| 紫云| 怀安县| 朝阳区| 时尚| 闵行区| 天水市| 方正县| 淳安县| 阿合奇县| 海林市| 永安市| 新干县| 青浦区| 宝鸡市| 柳州市| 辽宁省| 临高县| 白山市| 巧家县| 浙江省| 五峰| 内黄县| 个旧市| 盖州市| 聊城市| 两当县|