Introduced
Unique local database for Windows Phone 7.5 (SDK 7.1)
Overview
Demonstrates how to use the local database
Example
1. Overview
Summary.xaml
<phone: PhoneApplicationPage
x: Class = "Demo.LocalDatabase.Summary"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns: phone = "clr-namespace: Microsoft.Phone.Controls; assembly = Microsoft.Phone"
xmlns: shell = "clr-namespace: Microsoft.Phone.Shell; assembly = Microsoft.Phone"
xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily = "{StaticResource PhoneFontFamilyNormal}"
FontSize = "{StaticResource PhoneFontSizeNormal}"
Foreground = "{StaticResource PhoneForegroundBrush}"
SupportedOrientations = "Portrait" Orientation = "Portrait"
mc: Ignorable = "d" d: DesignHeight = "768" d: DesignWidth = "480"
shell: SystemTray.IsVisible = "True">
<Grid x: Name = "LayoutRoot" Background = "Transparent">
<TextBlock TextWrapping = "Wrap">
<Run> Overview of local database </ Run>
<LineBreak />
<LineBreak />
<Run> 1. When the App creates a database, its files will be saved to independent storage; the database in the package can only be read. If you need to update it, you must copy it to independent storage before operating </ Run>
<LineBreak />
<Run> 2. Prior to using DatabaseSchemaUpdater to update the database structure when the database structure changes; data migration is the next strategy </ Run>
<LineBreak />
<Run> 3. It is recommended to set ObjectTrackingEnabled of DataContext to false in read-only scenarios (because object tracking is not required when read-only), so as to turn off object tracking to reduce memory usage
<LineBreak />
<Run> 4. In the scenario of multi-threaded operation of the local database, it is recommended to use a mutex, namely System.Threading.Mutex </ Run>
</ TextBlock>
</ Grid>
</ phone: PhoneApplicationPage>
2, the use of "local database" Demo
Model Layer-ModelBase.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Data.Linq;
namespace Demo.LocalDatabase.Model
{
public class ModelBase: INotifyPropertyChanged, INotifyPropertyChanging
{
// INotifyPropertyChanged is implemented for notification after property changes
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged (string propertyName)
{
if (PropertyChanged! = null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
// INotifyPropertyChanging is implemented to minimize memory usage (Usage of NotifyPropertyChanging: Called before property assignment, specifically see Category or Product)
/ *
* Why reduce memory usage?
* Because LINQ to SQL change tracking works by maintaining two copies of each object, the first copy saves the original data and the second copy has program changes, so LINQ to SQL knows which data was changed when the update is submitted To submit only the changed data
* The INotifyPropertyChanging interface allows the application to notify the DataContext before submitting the modified data to the database. The DataContext can use this notification as a trigger to create a copy, so that there is no need to retain the second copy, thereby reducing memory usage
* /
public event PropertyChangingEventHandler PropertyChanging;
protected void NotifyPropertyChanging (string propertyName)
{
if (PropertyChanging! = null)
{
PropertyChanging (this, new PropertyChangingEventArgs (propertyName));
}
}
}
}