Data Access with WebMatrix
While the “current time” example helps show Razor in action within a WebMatrix page, real websites typically have a bit more content and logic. At its core, a weblog is just a list of content posts with metadata, which makes one a great context for beginners to use to learn the WebMatrix platform. Since WebMatrix web pages use Razor, developers have access to all of the features and functionality discussed earlier. The following sections leverage these Razor features along with WebMatrix’s powerful data access technology to build a real, working blog from start to finish.
Creating a Database
The content for the blog site—the blog posts—needs to be stored somewhere. Though we could create a new static page for each individual post, this approach will quickly become difficult to maintain. Instead, our blog will rely on the out-of-the-box WebMatrix database functionality to create a local database to store the site’s posts. In order to create a new database, switch to the Databases tab in your WebMatrix site, then click “Add a database to your site.”
This will add a new file in the site’s File List, named Empty Site.sdf. Rename this file to Blog.sdf. Directly underneath this new file, you will find an item named “Tables.” Right-click on this item and select the “New table” option, which will open a new tab in your Design view, containing a grid in which you can define your database table’s columns (see Figure 2-3).
Figure 2-3. WebMatrix table designer
Use this ...
Get Programming Razor now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.