Using a local database in the "Tidy" applicationVersion: 1.0.0 DescriptionThis 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. OverviewWindows® 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:
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. ObjectivesThis lab provides instructions to help you achieve the following:
PrerequisitesThe following prerequisites will ensure that you gain the most you can from this hands-on lab:
ArchitectureThe Tidy application was built using the MVVM pattern. MVVM dictates that each element of the application fits into one of three areas:
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 SQLLINQ 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 StructureThis lab includes a single exercise with seven tasks:
Estimated completion timeCompleting this lab should take between 20 and 40 minutes. |