Changing the database schema
Changes to your Windows Phone app may require changes to your local database schema. Any changes to your local database schema begin with changing the object model of the corresponding data context. TheMicrosoft.Phone.Data.Linq namespace provides theDatabaseSchemaUpdater class to help with database schema changes. For an example of how to use this class, seeWalkthrough: Updating a local database app for Windows Phone.
The DatabaseSchemaUpdater class can perform additive changes to the database, such as adding tables, columns, indexes or associations. For more complex changes, you will need to create a new database and copy the data to the new schema, as applicable. The DatabaseSchemaUpdater class provides aDatabaseSchemaVersion property that you can use to programmatically distinguish different versions of your database.
Important noteImportant Note:
The database does not change to reflect updates from the DatabaseSchemaUpdater object until theExecute method is called. When that method is called, all changes are submitted to the local database as a single transaction, including version updates. Using a single transaction helps the database maintain integrity, such as in cases where the user exits the app during an upgrade.
The following example demonstrates using the DatabaseSchemaUpdater class to modify the database based on theDatabaseSchemaVersion property.
using (ToDoDataContext db = new ToDoDataContext(("isostore:/ToDo.sdf")))
{
//Create the database schema updater
DatabaseSchemaUpdater dbUpdate = db.CreateDatabaseSchemaUpdater();
//Get database version
int dbVersion = dbUpdate.DatabaseSchemaVersion;
//Update database as applicable
if (dbVersion < 5)
{ //Copy data from existing database to new database
MigrateDatabaseToLatestVersion();
}
else if (dbVersion == 5)
{ //Add column to existing database to match the data context
dbUpdate.AddColumn<ToDoItem>("TaskURL");
dbUpdate.DatabaseSchemaVersion = 6;
dbUpdate.Execute();
}
}
还有方法这样描述:
// Prepopulate the categories.
// db.Categories.InsertOnSubmit(new ToDoCategory { Name = "Home" });
//db.Categories.InsertOnSubmit(new ToDoCategory { Name = "Work" });
// db.Categories.InsertOnSubmit(new ToDoCategory { Name = "Hobbies" });
// Save categories to the database.
// db.SubmitChanges();
Note:Any files saved in the local folder, including local database files, are not altered during the app update process.
截取自:http://msdn.microsoft.com/zh-cn/library/hh202860
Windows phone LocalDataBase 数据结构发生变化
最新推荐文章于 2024-07-14 01:31:01 发布