Navigation

Modify an Object Schema - .NET SDK

The following examples demonstrate how to add, delete, and modify properties in a schema. First, make the required schema change. Then, create a corresponding migration function to move data from the original schema to the updated schema.

Note
Versions Update on Realm Open

Assume that each schema change in this example occurs after an application has used each version for some amount of time. New schema version numbers only apply once you open the realm with an updated schema and explicitly specify the new version number, so in order to get to version 3, you would first need to open the app with versions 1 and 2.

A realm using schema version 1 has a Person object type:

public class Person : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public int Age { get; set; }
}

The following example adds a LastName property to the original Person schema:

public class Person : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

The following example uses a combined FullName property instead of the separate FirstName and LastName property in the original Person schema:

public class Person : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}

The following example modifies the Age property in the original Person schema by renaming it to Birthday and changing the type to DateTimeOffset:

public class Person : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; }
public string FullName { get; set; }
public DateTimeOffset Birthday { get; set; }
}

To migrate the realm to conform to the updated Person schema, the set the realm's schema version to 4 and define a migration function to set the value of FullName based on the existing FirstName and LastName properties and the value of Birthday based on Age:

var config = new RealmConfiguration
{
SchemaVersion = 4,
MigrationCallback = (migration, oldSchemaVersion) =>
{
var oldPeople = migration.OldRealm.All<Person>();
var newPeople = migration.NewRealm.All<Person>();
// Migrate Person objects
for (var i = 0; i < newPeople.Count(); i++)
{
var oldPerson = oldPeople.ElementAt(i);
var newPerson = newPeople.ElementAt(i);
// Changes from version 1 to 2 (adding LastName) will occur automatically when Realm detects the change
// Migrate Person from version 2 to 3: replace FirstName and LastName with FullName
// LastName doesn't exist in version 1
if (oldSchemaVersion < 2)
{
newPerson.FullName = oldPerson.FirstName;
}
else if (oldSchemaVersion < 3)
{
newPerson.FullName = $"{oldPerson.FirstName} {oldPerson.LastName}";
}
// Migrate Person from version 3 to 4: replace Age with Birthday
if (oldSchemaVersion < 4)
{
var birthYear = DateTimeOffset.UtcNow.Year - oldPerson.Age;
newPerson.Birthday = new DateTimeOffset(birthYear, 1, 1, 0, 0, 0, TimeSpan.Zero);
}
}
}
};
var realm = Realm.GetInstance(config);
Tip
Give Feedback