Using the AddTable
method allows you to create a new table for an entity class. On adding a new table, the DatabaseSchemaUpdater
also applies the metadata associated with the entity class and creates any related indexes or foreign key constraints.
To add a new entity to the Twitter data model, we first create that entity and decorate it with Table
and Column
attributes. In the following example, the TwitterFriend
class represents a Twitter user, who happens to be a contact of another Twitter user (see Listing 29.10). TwitterFriend
contains an Id
and a ScreenName
property. In addition, an index is declared for the ScreenName
property.
[Table]
[Index(Columns = "ScreenName")]
public class TwitterFriend : NotifyPropertyChangeBase
{
string id;
[Column(IsPrimaryKey = true)]
public string Id
{
get
{
return id;
}
set
{
Assign(ref id, value);
}
}
string screenName;
[Column]
public string ScreenName
{
get
{
return screenName;
}
set
{
Assign(ref screenName, value);
}
}
}
The following code is used to upgrade the schema to include a new table for storing TwitterFriend
data:
using (DataContext dataContext
= new TwitterDataContext("isostore:Twitter.sdf"))
{
DatabaseSchemaUpdater updater
= dataContext.CreateDatabaseSchemaUpdater();
updater.AddTable<TwitterFriend>();
updater.Execute();
}
It can be seen from Figure 29.10 that after execution, the Twitter database contains the new table, with a secondary index on the ScreenName
column, as desired.
18.221.19.26