Let's talk a few more minutes about the WCF
Data Service you built and how you can use that to navigate through
records. Record navigation is one of the things that really gets me
excited about WCF Data Services. Let's dive right in.
If your project is still
running, stop the project and open the Solution Explorer and navigate to
the data service. For simplicity, you'll do this right from the
solution. Right mouse click the data service and select View in Browser.
The service will fire up and what you see is a REST (Representational
State Transfer) based service on top of your relational database, a mere
XML representation of the service and the entities exposed via the
service, shown in Figure 1.
You see the entities listed because you set the entity set rights to
ALL. If you were to go back to the code a few pages back where you set
the entity set rights, and comment those lines out, you would not see
the entities listed (Docs, TechGeoInfoes, and Users).
The question though, is how do
you view the data that you really want to see. The answer is simple.
Specify an entity at the end of your URI, and you'll see data for that
entity. For example, specify the following URI to see data on the Users entity:
http://localhost:51176/TechBioDataService.svc/Users
Take care to get the case correct. Entity names are case sensitive. Specify users instead of Users, and you'll get a "The webpage cannot be found" error message.
Take time out now to mistype an
entity name and see the resulting message. That way you'll more readily
recognize the problem when you make same mistake inadvertently. Sooner
or later, you all make that mistake.
|
|
1. Disabling Internet Explorer's Feed Reading View
At this point you either get an XML representation of the data in the Users table, or the web page shown in Figure 2.
If it's the latter, then you need to go turn off the feed reading view
in Internet Explorer IE. That is because IE thinks that the data coming
back is the type you would get in an RSS feed. You can see in the
message that the browser thinks you're trying to view an RSS feed.
To fix the RSS feed issue you
need to turn off this feature in Internet Explorer. With IE open, from
the Tools menu select Options which open the Internet Options dialog.
This dialog has a number of tabs along the top which you might be
familiar with. Select the Content tab and on that tab click the Settings
button under Feeds and Web Slices.
Clicking the Settings
button will display the Settings dialog, and on this dialog you need to
uncheck the Turn on feed reading view checkbox, shown in Figure 3. Click OK on this dialog and the Internet Options dialog.
2. Viewing the Final Results
Back on your web page, press F5 to refresh the page. What you should get back now is a collection of Users, shown in Figure 4, by querying the underlying database for the Users.
However, you aren't done yet
because there is still so much more you can do here. For example, the
page you're currently looking at displays all the Users, but what if you
want to return a specific user?
Looking at the data you can
see that the each record contains the id of the specific row, and you
can use that to your advantage by including that in your URI. For this
example let's use ID 113. Modify the URI by appending the number 113 to
the end of the URI enclosed in parenthesis, as shown in Figure 5.
By loading the URI which
includes the id of a specific record, I can now drill down further and
return just the record I am looking for. This is just like applying a WHERE clause to a T-SQL query, in this case WHERE ID = 113. In this case I have queried the underlying store for a specific user by passing the appropriate ID in the URI.
Additionally I can return a specific field by adding the field I want to the URI, such as:
http://localhost:51176/TechBioDataService.svc/Users(113)/Name
Specifying the specific
field along with the id will return just the field you request. In the
code snipped above, the value in the Name column for User ID 113 is
returned, as shown in Figure 6.
You can also use this same
methodology to navigate between tables. For example, you could do the
following to return documents for a specific User ID:
http://localhost:51176/TechBioDataService.svc/Users(113)/Docs
While this information isn't
critical to connect to SQL Azure it's good information to have so you
know how REST services work and can benefit from its functionality in
your application. While this article did not go deep into the Entity
Framework or REST technology, there are plenty of good books by APress
or information on MSDN about this technology. I highly recommend that
you explore these technologies further to enhance your SQL Azure
applications.