I found the problem. I fixed it by making a change in the Disconnected Users Tutorial. The RestoreEntitySet operation will attempt to merge the entities in the offline storage file with the PersistenceManager cache. If it sees that PersistenceManager.IsConnected property is true, it will attempt to InitializeSchema from the backend database, and this will result in an exception because it cannot connect to the database. I fixed the problem simply with a PersistenceManager.Disconnect before issuing the ResetoreEntitySet. Here is the code:
private void LoadOffLine() {
String path = Application.UserAppDataPath + "\\MyLocalData.bin";
if (File.Exists(path)) {
try {
if (!mConnectedToDatabase)
mPersMgr.Disconnect();
mPersMgr.RestoreEntitySet(path);
LoadData();
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
File.Move(path, path + "\\" + DateTime.Now.ToString());
MessageBox.Show("Local data file not found!");
}
}
}
The reason the LoadOffLine operation worked immediately after opening the application is that the PersistenceManager was already disconnected in this case.