Programming4us
         
 
 
SQL Server

Programming with SQL Azure : WCF Data Services (part 1)

1/6/2011 3:18:55 PM
WCF Data Services, formerly known as ADO.NET Data Services, enables the creation and consumption of OData services. OData, the Open Data Protocol, is a new data-sharing standard that allows for greater sharing of data between different systems. Before it was called WCF Data Services, ADO.NET Data Services was one of the very first Microsoft technologies to support OData with Visual Studio 2008 SP1. Microsoft has broadened its support of OData in products such as SQL Server 2008 R2, Windows Azure Storage, and others. This section discusses how to use WCF Data Services to connect to and query your SQL Azure database.

1. Creating a Data Service

First you need to create a data service. Follow these steps:

  1. Create a new ASP.NET web application, and call it WCFDataServiceWebApp. (You can host data services a number of different environments but this example uses a web app.)

  2. The next step in creating a data service on top of a relational database is to define a model used to drive your data service tier. The best way to do that is to use the ADO.NET Entity Framework, which allows you to expose your entity model as a data service. And to do that, you need to add a new item to your web project. Right-click the web project, and select New Item. In the Add New Item dialog, select Data from the Categories list, and then select ADO.NET Entity Data Model from the Templates list. Give the model the name TechBioModel.edmx, and click OK.

  3. In the first step of the Data Model Wizard, select the Generate From Database option, and click Next.

  4. The next step is Choose Your Data Connection. Click the New Connection button, and create a connection to your SQL Azure database. Save the entity connection settings as TechBioEntities, and then click Next.

  5. The next step of the wizard is the Choose Your Database Objects page. Select all the tables. Note the option that is new to ADO.NET Entity Framework version 4.0, which pluralizes or singularizes generated objects names. If you leave this option checked, it comes into play later. Leave it checked, and click Finish.

  6. The Entity Framework looks at all the tables you selected and creates a conceptual model on top of the storage schema that you can soon expose as a data service. In Visual Studio, you should see the Entity Framework Model Designer with a graphical representation of the tables, called entities, and their relationships. Close the Model Designer—you don't need it for this example.

  7. What you need to do now is create the data service on top of your data model. In Solution Explorer right-click the web application and select Add, then select New Item. In the Add New Item dialog, select the Web category, then scroll down the list of templates and select the WCF Data Service template. Enter a name of TechBioDataService the click Add, as shown in Figure 1

    Figure 1. Adding a WCF Data Service to the Solution

When the ADO.NET Data Service is added to your project, the associated .cs file will automatically be displayed in the IDE. As you can see the ADO.NET Data Service template has generated for you the beginnings of your data service.

2. Connecting the Service to the Model

Now you need to wire up your data service to your data model so that the service knows where to get its data. You know where to do this because as you can see in the code it tells you where to enter that information. Thus, change the line:

public class TechBioDataService : DataService< /* TODO: put your data source
class name here */ >

To:
public class TechBioDataService : DataService< TechBioEntities >

Wiring up your data service to the model is as simple as that. Believe it or not, you're ready to test your service. However, let's finish what you need to do on this page. By default, the WCF Data Service is secured. The WCF Data Service needs to be told explicitly which data you want to see. The instructions in the code tell you this, as you can see in code in the InitializeService method. Some examples are even provided in the comments to help you out.

For your example, you don't want to restrict anything so you really want to unlock all the entities and explicitly define access rights to the entity sets. You do this by adding the code below to the InitializeService method. The code below sets the access rule for the specified entities to All, providing authorization to read, write, delete, and update data for the specified entity set:

// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("Docs", EntitySetRights.All);
config.SetEntitySetAccessRule("UserDocs", EntitySetRights.All);
config.SetEntitySetAccessRule("Users", EntitySetRights.All);
config.SetEntitySetAccessRule("TechGeoInfo", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}


If you don't feel like specifying each entity one by one, you can optionally specify all the entities with a single line, as follows:

config.SetEntitySetAccessRule("*", EntitySetRights.All);

The above line assumes that you want to specify the same rights to all the entities. Not recommended, but will do for this example. In a production environment you want to more specific with what rights you specify for each entity.

There are other EntitySetRights options, such as AllRead, AllWrite, None, ReadSingle, and WriteAppend. You won't cover them all here but you can read about them here:

http://msdn.microsoft.com/en-us/library/system.data.services.entitysetrights.aspx

So far, you've created your Web application, added your data model, and added your WCF Data Service. Right now your Solution Explorer should look like Figure 2.
Figure 2. Solution Explorer
Other -----------------
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 2) - Working with Binary Columns
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 1) - RAW Mode
- Programming with SQL Azure : Connecting to SQL Azure (part 4) - Sqlcmd
- Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
- SQL Server 2008: Troubleshooting SSB Applications with ssbdiagnose.exe
- SQL Server 2008: Service Broker Routing and Security
- Migrating Databases and Data to SQL Azure (part 9)
- Migrating Databases and Data to SQL Azure (part 8)
- Understanding Service Broker Constructs (part 5)
- Understanding Service Broker Constructs (part 4) - Creating the Conversation Initiator
 
 
Most View
- Windows 7 : Using Volume Activation (part 2) - Volume Activation Scenarios
- Windows Home Server 2011 : Setting Up a Fax Server (part 2) - Sending a Fax
- Windows Phone 7 : Locking Your Phone
- jQuery 1.3 : Compact forms (part 4)
- SQL Server 2012 : SQL Server Architecture - THE LIFE CYCLE OF A QUERY (part 2) - Plan Cache
- An OLAP Requirements Example: CompSales International (part 6) - Creating the Cube
- Monitoring SQL Server 2005 Performance : Monitoring and Recording Performance
- .NET Debugging : Introduction to the Tools - SOS & SOSEX
- Windows Small Business Server 2011 : Understanding the Update Process (part 2) - Monitoring WSUS Activity
- Windows 7 : Defragmenting Your Hard Disk
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS