http://msdn.microsoft.com/en-us/library/hh202860(v=vs.92).aspx
December 15, 2011
With Windows Phone OS 7.1, you can store relational data in a local database that resides in your application’s isolated storage container. Windows Phone applications use LINQ to SQL for all database operations; LINQ to SQL is used to define the database schema, select data, and save changes to the underlying database file residing in isolated storage. This topic provides an overview of using a local database with your Windows Phone application. For a step-by-step walkthrough of creating an application that uses a local database, see How to: Create a Basic Local Database Application for Windows Phone.
|
|
|---|
LINQ to SQL on Windows Phone does not directly support executing Transact-SQL, including Data Definition Language (DDL) or Data Modeling Language (DML) statements. Additionally, Windows Phone applications cannot use LINQ to SQL to directly access ADO.NET objects. For more information, see LINQ to SQL Support for Windows Phone. |
To deploy reference data with your application, you need to perform the following steps:
Create the helper application: The helper application runs on your development computer, creates the local database in isolated storage, and loads the database with the desired reference data.
Extract the local database from the helper application: Use the Isolated Storage Explorer (ISETool.exe) to copy the database from the helper application to a folder on your computer. For more information about the Isolated Storage Explorer, see How to: Use the Isolated Storage Explorer Tool.
Create the primary application: Create the application that will consume the reference data.
Add the reference data to the primary application: Use Visual Studio to add the local database file to the primary application from the folder where you saved it on your computer. To minimize the size of the application’s assembly, store the file as Content.
After a local database is deployed with an application, it resides in the installation folder in a read-only state. The installation folder is different than isolated storage. To address the database file in this location, use the appdata: prefix. For an example of using this prefix with the database connection string, see Local Database Connection Strings for Windows Phone.
To modify the database containing the reference data, move it out of the installation folder and save it in isolated storage before attempting database changes. To move the database file, you can perform a stream-based copy with the Application.GetResourceStream method to create a stream from the installation folder, and the IsolatedStorageFileStream.Write method to write the stream to isolated storage. The following example demonstrates how to address a database file in the installation folder when you create a stream object.
For each entity, mapping details are specified by using LINQ to SQL mapping attributes. These attributes specify database-specific features such as tables, columns, primary keys, and indexes. For more information, see Attribute-based Mapping (LINQ to SQL). For example, the following code shows a data context named ToDoDataContext and the beginning of an entity class named ToDoItem.
|
|
|---|
This is only a portion of the data context code. For a step-by-step walkthrough of creating an application that uses a local database, see How to: Create a Basic Local Database Application for Windows Phone. |
To use local database features in your code, you will need the following directives at the top of your code file.
Some common LINQ to SQL mapping attributes are shown in the following table. For a full list, see System.Data.Linq.Mapping Namespace.
Attribute |
Example |
Description |
|---|---|---|
TableAttribute |
[Table] |
Designates a class as an entity class that is associated with a database table. |
ColumnAttribute |
[Column(IsPrimaryKey = true)] |
Associates a class with a column in a database table. IsPrimaryKey specifies the primary key, for which an index is created by default. |
IndexAttribute |
[Index(Columns="Column1,Column2 DESC", IsUnique=true, Name="MultiColumnIndex")] |
Written at the table level, designates additional indexes on the table. Each index can cover one or more columns. |
AssociationAttribute |
[Association(Storage="ThisEntityRefName", ThisKey="ThisEntityID", OtherKey="TargetEntityID")] |
Designates a property to represent an association, such as a foreign key to primary key association. |