Chapter 29. Storing App Data in a Local Database

,

In This Chapter

Image Code-first and database-first data model creation

Image Differences between isolated storage and XAP file deployment

Image Change tracking with LINQ to SQL

Image Defining one-to-many relationships

Image Retrieving a local database from isolated storage

Image Using SqlMetal to generate your data model

Image Deploying a prepopulated database to isolated storage

Image A testable navigation service

Image Schema versioning

Image Mapping inheritance hierarchies

Image Controlling optimistic concurrency

Image A Twitter timeline viewer


For some apps, having to rely on a cloud service for storing application data is not practical. Some scenarios cry out for local storage of structured data on the phone.

With the Mango release of Windows Phone (7.5), Microsoft introduced a relational database system to the phone, allowing you to place a local database in either isolated storage or in your app’s XAP file.

Windows Phone apps use LINQ to SQL for all database operations; LINQ to SQL is used to define the database schema and to select, insert, update, and delete data from the database. LINQ to SQL provides an object-oriented approach to working with relational data and is composed of an object model and a runtime. With LINQ to SQL, classes represent database tables, and foreign key relationships between tables are represented by aggregated collections.

This chapter begins with an overview of how local databases are deployed to the phone and discusses the main elements of LINQ to SQL.

The chapter then explores the code-first approach to data model creation and walks through a sample Twitter timeline viewer that caches data in a local database, during which you explore several facets of LINQ to SQL, including entity creation, change tracking, and entity multiplicity (one-to-many relationships).

The database-first approach to data model creation is then discussed. You see how to retrieve a local database file from isolated storage and how to use the SqlMetal tool to generate a data model from a database.

The chapter then takes a short deviation and examines a custom navigation service that allows navigation from a viewmodel without being coupled to the built-in navigation of the phone, which can increase the testability of your viewmodel classes.

The chapter then gets back on topic to examine a technique for observing LINQ to SQL queries using a custom log. You see how to upgrade a schema without losing data, and you learn how to use version numbering to provide incremental updates to your database schema.

Finally, you look at mapping inheritance hierarchies using LINQ to SQL and at concurrency and conflict detection when performing updates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.67.125