Class Migration
This class is given to you when you migrate your database from one version to another. It contains two properties: OldRealm and NewRealm. The NewRealm is the one you should make sure is up to date. It will contain models corresponding to the configuration you've supplied. You can read from the OldRealm and access properties that have been removed from the classes by using the dynamic API.
Namespace: Realms
Assembly: Realm.dll
Syntax
public class Migration
Properties
| Edit this page View SourceNewRealm
Gets the Realm that you should modify and make sure is up to date.
Declaration
public Realm NewRealm { get; }
Property Value
Type | Description |
---|---|
Realm | The Realm that will be saved after the migration. |
OldRealm
Gets the Realm as it was before migrating. Use the dynamic API to access it.
Declaration
public Realm OldRealm { get; }
Property Value
Type | Description |
---|---|
Realm | The Realm before the migration. |
Methods
| Edit this page View SourceFindInNewRealm<T>(IRealmObject)
Declaration
public T? FindInNewRealm<T>(IRealmObject obj) where T : IRealmObject
Parameters
Type | Name | Description |
---|---|---|
IRealmObject | obj | The object obtained from the old realm. |
Returns
Type | Description |
---|---|
T | The corresponding object post-migration or |
Type Parameters
Name | Description |
---|---|
T | The type of the object in the new realm. |
Examples
foreach (var oldPerson in migration.OldRealm.DynamicApi.All("Person"))
{
var newPerson = migration.FindInNewRealm<Person>(oldPerson)
newPerson.Name = $"{oldPerson.DynamicApi.Get<string>("FirstName")} {oldPerson.DynamicApi.Get<string>("LastName")}";
}
|
Edit this page
View Source
RemoveType(string)
Removes a type during a migration. All the data associated with the type, as well as its schema, will be removed from Realm.
Declaration
public bool RemoveType(string typeName)
Parameters
Type | Name | Description |
---|---|---|
string | typeName | The type that needs to be removed. |
Returns
Type | Description |
---|---|
bool |
|
Remarks
The removed type will still be accessible from OldRealm in the migration block. The type must not be present in the new schema. RemoveAll<T>() can be used on NewRealm if one needs to delete the content of the table.
RenameProperty(string, string, string)
Renames a property during a migration.
Declaration
public void RenameProperty(string typeName, string oldPropertyName, string newPropertyName)
Parameters
Type | Name | Description |
---|---|---|
string | typeName | The type for which the property rename needs to be performed. |
string | oldPropertyName | The previous name of the property. |
string | newPropertyName | The new name of the property. |
Examples
// Model in the old schema
class Dog : RealmObject
{
public string DogName { get; set; }
}
// Model in the new schema
class Dog : RealmObject
{
public string Name { get; set; }
}
//After the migration Dog.Name will contain the same values as Dog.DogName from the old realm, without the need to copy them explicitly
var config = new RealmConfiguration
{
SchemaVersion = 1,
MigrationCallback = (migration, oldSchemaVersion) =>
{
migration.RenameProperty("Dog", "DogName", "Name");
}
};