The DatabaseSchemaUpdater
class provides a DatabaseSchemaVersion
property that allows you to programmatically distinguish between different versions of your database.
When a local database is created with the DataContext.CreateDatabase
method, a table named _Version is automatically created. This table holds the current DatabaseSchemaVersion
number.
In the following example, the database version is used to determine what needs to be done to update the schema to the current version:
using (TwitterDataContext dataContext
= twitterDatabaseUtility.CreateContext())
{
DatabaseSchemaUpdater updater
= dataContext.CreateDatabaseSchemaUpdater();
int databaseVersion = updater.DatabaseSchemaVersion;
if (databaseVersion < 2)
{
updater.AddColumn<TwitterUser>("Homepage");
updater.DatabaseSchemaVersion = 2;
updater.Execute();
}
if (databaseVersion < 3)
{
updater.AddColumn<TwitterUser>("Birthday");
updater.DatabaseSchemaVersion = 3;
updater.Execute();
}
}
As you make changes to your schema, take note of them. This eases the burden of writing schema versioning code later.
3.148.106.212