Migrations - adhtalbo/RedisContext GitHub Wiki

If you change an entities properties you may need a script to update any values in the database with the old format. The way this is handled is with versioning and migration functions.

You can specify the entity version with the VersionAttribute. If no VersionAttribute is set the version is zero. You only need to increment the version number if the change requires the old data to be manipulated to fit the new entity, adding/removing fields can be done without an increment.

Migrations are methods attributed with the MigrationAttribute. They should receive a single parameter of EntityProperties. EntityProperties can be accessed like a Dictionary to retrieve values. Values are of type EntityProperty.

There one main way to retrieve the value of an EntityProperty. EntityProperty.TryGet() can be used to attempt to get the value in a certain type. This method returns true if value could be retrieved in the requested type. You can also use EntityProperty.IsTypeOf() can be used to check if a value is of a certain type.

For example if the original entity looks like this

public class BasicEntity : RedisEntity
{
    public string SomeValue {get; set;}
    
    public int AnotherValue {get; set;}
}

And you with to rename SomeValue to RenamedValue then the new entity would look like this

[Version(1)]
public class BasicEntity : RedisEntity
{
    public string RenamedValue {get; set;}
    
    public int AnotherValue {get; set;}

    [Migrate(0, 1)]
    private void MigrateToV1(EntityProperties properties)
    {
        if(properties.ContainsKey("SomeValue"))
        {
            string someValue;
            properties["SomeValue"].TryGet(someValue);
            RenamedValue = someValue;
        }
        
    }
}