Whereas DataContext
enables you to create and delete a database file, a custom class, called DatabaseUtility
, is used to directly create and delete the database in isolated storage and to build the connection string for the database.
TwitterDatabaseUtility
extends the DatabaseUtility
class, providing the default database filename. TwitterDatabaseUtility
is used in various locations to create a DataContext
for the Twitter database.
DatabaseUtility
includes an InitializeDatabase
method that creates the isolated storage file for the database. When it is called, if the wipe
parameter is true and the database already exists, it is deleted before being re-created. See the following excerpt:
public void InitializeDatabase(DataContext dataContext, bool wipe = false)
{
ArgumentValidator.AssertNotNull(dataContext, "dataContext");
if (localDatabaseMode != LocalDatabaseMode.ReadWrite
&& localDatabaseMode != LocalDatabaseMode.Exclusive)
{
return;
}
if (wipe && dataContext.DatabaseExists())
{
dataContext.DeleteDatabase();
}
if (!dataContext.DatabaseExists())
{
dataContext.CreateDatabase();
}
}
Initialization of the local database occurs when the Launching
event of the PhoneApplicationService
is raised. For testing purposes, if a debugger is attached, the database is deleted so as to begin with a clean slate. The following excerpt shows the Launching
event handler:
void Application_Launching(object sender, LaunchingEventArgs e)
{
Debug.WriteLine("Application Launching");
StateManager.Initialize();
/* For the Twitter demo. */
var twitterDatabaseUtility = new TwitterDatabaseUtility();
bool wipeDatabase = Debugger.IsAttached;
twitterDatabaseUtility.InitializeDatabase(wipeDatabase);
}
When the app is launched, the handler ensures that the database file is created in isolated storage.
3.22.51.241