Using a local database in the "Tidy" application

Version: 1.0.0

Description

This hands-on-lab will show you how to use SQL Compact Edition (SQL CE) to build a task management application. You will first learn how to apply the right attributes to an existing object model so that the DataContext class can create a relational database with keys and constraints. Then, you will also learn how to query the database using LINQ-to-SQL.

Overview

Windows® Phone Codenamed Mango provides a new way to store and manage your application’s structured data - Microsoft SQL Server Compact edition databases. The database exists as a file stored in the application’s isolated storage. This lab uses the Tidy application to demonstrate how to work with databases when writing applications for Windows® Phone. The application manages projects with associated tasks. Each task supports attachments as well as a predefined set of data including priorities and due dates. All this information is persisted by using a local database with several main tables:

  • Projects
  • Tasks
  • Attachments
  • Locations

The following image illustrates the data contained in the entities supported by the application and the relationships between these entities.

Figure 1

Database entities and their associations

Window Phone Codenamed Mango applications use the LINQ to SQL APIs in order to access local databases. LINQ to SQL provides an object oriented approach for manipulating the data stored in the database. The main class responsible for bridging the application’s object model with the data contained in the database is the System.Data.Linq.DataContext class.

Objectives

This lab provides instructions to help you achieve the following:

  • Develop applications leveraging local databases on Windows Phone Codenamed Mango
  • Understand the implementation of local databases in the Tidy Application

Prerequisites

The following prerequisites will ensure that you gain the most you can from this hands-on lab:

  • Microsoft Visual Studio 2010 or Microsoft Visual C# Express 2010, and the Windows® Phone Developer Tools available at https://go.microsoft.com/?linkid=9772716
  • Knowledge on how to create applications for Windows Phone 7
  • Familiarity with basic SQL terms

Architecture

The Tidy application was built using the MVVM pattern. MVVM dictates that each element of the application fits into one of three areas:

  • View – Presents the user with an interface and passes the user’s actions to the view model
  • ViewModel – Bridges between the user’s actions and the application’s data
  • Model – Manages the application’s data

This partitioning works in the Tidy application in the following manner:

The DataContextBase class, which inherits from the System.Data.Linq.DataContext class mentioned earlier, implements the model. This class, along with the other classes it references, allows performing operations on local application objects in order to affect the data contained in the application’s database.

The ViewModelItemsBase class and the ViewModelBase class (along with the subclasses for both of these) implement the ViewModel. These classes hide the model itself from the user interface by providing simpler access to the model’s contents as well as by simplifying data manipulation operations such as defining new tasks, updating projects, etc. The various application pages that form the application’s user interface implement the view.

LINQ to SQL

LINQ to SQL is a .Net framework ORM (Object Relational Mapping) platform that maps between the database (which only "speaks" transact-SQL), and application objects. When the application executes a LINQ statement at runtime, it translates it into transact-SQL and executes it against the database. Once the database returns a result for the query, LINQ to SQL translates it back to application objects.

To learn more about LINQ to SQL please go https://msdn.microsoft.com/en-us/library/bb425822.aspx

Note:
NOTE: If you are familiar with LINQ to SQL on the desktop, please note that some LINQ to SQL features are not available on the Windows Phone implementation. This implementation of LINQ to SQL does not support "raw" transact-SQL, DML (Data Modeling Language), DDL (Data Definition Language), and does not provide direct access to ADO.NET objects.

Lab Structure

This lab includes a single exercise with seven tasks:

  1. Defining a data context
  2. Creating a database
  3. Deploying an existing database
  4. Getting data
  5. Inserting data
  6. Updating data
  7. Deleting data

Estimated completion time

Completing this lab should take between 20 and 40 minutes.