AddColumn
allows you to add a new column to an existing table. For example, if you added a new property to the TwitterUser
class to record the user’s age, you would then need to use the DatabaseSchemaUpdater
to create a new column to store the Age
value. This could be achieved in the following manner:
using (DataContext dataContext
= new TwitterDataContext("isostore:Twitter.sdf"))
{
DatabaseSchemaUpdater updater
= dataContext.CreateDatabaseSchemaUpdater();
updater.AddColumn<TwitterUser>("Age");
updater.Execute();
}
Note
The AddColumn
method only allows columns to be added that can contain null values. Therefore, decorating a property with [Column(CanBeNull = false)]
prevents the AddColumn
method from succeeding and causes an SqlCeException
to be raised when the DatabaseSchemaUpdater.Execute
method is called.
The database does not change to reflect updates from the DatabaseSchemaUpdater
until its Execute
method is called, at which point all changes are submitted to the local database as a single transaction, which ensures the database maintains integrity if it is interrupted, such as when the user exits the app during an upgrade.
18.218.163.28