Skip to content

Chronicle event type migrations in C#

To evolve an event schema with the Cratis Chronicle .NET client, keep a record type for each generation and define a migrator between each adjacent pair. The client discovers these declarations and registers them with the kernel, which applies the transformations when events are appended.

  • A Chronicle-enabled .NET application
  • The existing event record and the new schema it needs to support

This guide uses attributes to identify event generations. If your contracts must stay free of client attributes, use explicit registration instead. Before deploying a new generation, follow the kernel rollout guidance.

Keep the old record and introduce the new shape with an explicit generation number. Both records represent the same event type: Chronicle connects them through their event type id, not their C# class names.

using Cratis.Chronicle.Events;
// Generation 2 (current) — Name has been split into FirstName and LastName
[EventType("dotnet-client-author-registered", generation: 2)]
public record MigrationsDotnetClientAuthorRegistered(string FirstName, string LastName);
// Generation 1 (original) — marked as a previous generation of the current record above,
// instead of carrying its own [EventType]
[EventTypeGenerationFor<MigrationsDotnetClientAuthorRegistered>(1)]
public record MigrationsDotnetClientAuthorRegisteredV1(string Name);

View C# snippet source on GitHub

Generation 2 splits Name into FirstName and LastName. Its record, MigrationsDotnetClientAuthorRegistered, declares the stable id with [EventType]. The older MigrationsDotnetClientAuthorRegisteredV1 uses [EventTypeGenerationFor<MigrationsDotnetClientAuthorRegistered>(1)] to take its id from that declaration. You can rename either record without maintaining two copies of the id string.

Extend EventTypeMigration<TUpgrade, TPrevious> with the newer generation first and the older generation second. Declare the older-to-newer transformation in Upcast and the reverse in Downcast:

using Cratis.Chronicle.Events.Migrations;
public class MigrationsDotnetClientAuthorRegisteredMigration : EventTypeMigration<MigrationsDotnetClientAuthorRegistered, MigrationsDotnetClientAuthorRegisteredV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientAuthorRegistered, MigrationsDotnetClientAuthorRegisteredV1> builder) =>
builder.Properties(pb => pb
.Split(m => m.FirstName, e => e.Name, PropertySeparator.Space, SplitPartIndex.First)
.Split(m => m.LastName, e => e.Name, PropertySeparator.Space, SplitPartIndex.Second));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientAuthorRegisteredV1, MigrationsDotnetClientAuthorRegistered> builder) =>
builder.Properties(pb => pb
.Combine(m => m.Name, PropertySeparator.Space, e => e.FirstName, e => e.LastName));
}

View C# snippet source on GitHub

Here, Upcast splits the old Name, while Downcast combines the new fields into the shape generation 1 expects. The base class reads From and To from the endpoint attributes, whether [EventType] or [EventTypeGenerationFor<T>]; you do not set the numbers again in the migrator.

The base constructor rejects different event type ids with MigrationGenerationsMustShareEventTypeId and non-adjacent generations with InvalidMigrationGenerationGap. Each migrator must bridge exactly one step: To == From + 1.

With the default discovery setup, migrators are found at startup without explicit registration. Keep one migrator for each transition. If two classes claim the same generation pair for one event type, EventTypeMigrators.GetMigratorsFor throws MultipleMigratorsForSameEventTypeGeneration.

Declare property operations inside builder.Properties(pb => ...). The target expression names a property in the generation you are producing; source expressions refer to the generation you are reading. DefaultValue supplies a literal instead of reading a source property. See Event Type Migrations for the operation semantics.

Extracts one segment of a string property by splitting on a separator and taking the part at a given index.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType("dotnet-client-person-registered", generation: 2)]
public record MigrationsDotnetClientSplitPersonRegistered(string FirstName, string LastName);
[EventTypeGenerationFor<MigrationsDotnetClientSplitPersonRegistered>(1)]
public record MigrationsDotnetClientSplitPersonRegisteredV1(string FullName);
public class MigrationsDotnetClientSplitPersonRegisteredMigration : EventTypeMigration<MigrationsDotnetClientSplitPersonRegistered, MigrationsDotnetClientSplitPersonRegisteredV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientSplitPersonRegistered, MigrationsDotnetClientSplitPersonRegisteredV1> builder) =>
builder.Properties(pb => pb
.Split(t => t.FirstName, s => s.FullName, PropertySeparator.Space, SplitPartIndex.First)
.Split(t => t.LastName, s => s.FullName, PropertySeparator.Space, SplitPartIndex.Second));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientSplitPersonRegisteredV1, MigrationsDotnetClientSplitPersonRegistered> builder) =>
builder.Properties(pb => pb
.Combine(t => t.FullName, PropertySeparator.Space, s => s.FirstName, s => s.LastName));
}

View C# snippet source on GitHub

PropertySeparator.Space is a built-in constant. Any string is also implicitly convertible to a PropertySeparator, so ":" works directly for colon-delimited fields. SplitPartIndex.First (index 0) and SplitPartIndex.Second (index 1) cover the most common cases; pass any int for deeper splits.

Concatenates multiple source properties into a single string target property, joining with a separator. The second argument is a PropertySeparator — use the built-in PropertySeparator.Space or pass any string (e.g. ":").

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType("dotnet-client-shipping-address-recorded", generation: 2)]
public record MigrationsDotnetClientCombineShippingAddressRecorded(string FullAddress);
[EventTypeGenerationFor<MigrationsDotnetClientCombineShippingAddressRecorded>(1)]
public record MigrationsDotnetClientCombineShippingAddressRecordedV1(string Street, string City);
public class MigrationsDotnetClientCombineShippingAddressRecordedMigration : EventTypeMigration<MigrationsDotnetClientCombineShippingAddressRecorded, MigrationsDotnetClientCombineShippingAddressRecordedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientCombineShippingAddressRecorded, MigrationsDotnetClientCombineShippingAddressRecordedV1> builder) =>
builder.Properties(pb => pb
.Combine(t => t.FullAddress, PropertySeparator.Space, s => s.Street, s => s.City));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientCombineShippingAddressRecordedV1, MigrationsDotnetClientCombineShippingAddressRecorded> builder) =>
builder.Properties(pb => pb
.Split(t => t.Street, s => s.FullAddress, PropertySeparator.Space, SplitPartIndex.First)
.Split(t => t.City, s => s.FullAddress, PropertySeparator.Space, SplitPartIndex.Second));
}

View C# snippet source on GitHub

Maps a property from its old name in the source generation to its new name in the target generation.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType("dotnet-client-customer-registered", generation: 2)]
public record MigrationsDotnetClientRenamedFromCustomerRegistered(string Email);
[EventTypeGenerationFor<MigrationsDotnetClientRenamedFromCustomerRegistered>(1)]
public record MigrationsDotnetClientRenamedFromCustomerRegisteredV1(string EmailAddress);
public class MigrationsDotnetClientRenamedFromCustomerRegisteredMigration : EventTypeMigration<MigrationsDotnetClientRenamedFromCustomerRegistered, MigrationsDotnetClientRenamedFromCustomerRegisteredV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientRenamedFromCustomerRegistered, MigrationsDotnetClientRenamedFromCustomerRegisteredV1> builder) =>
builder.Properties(pb => pb
.RenamedFrom(t => t.Email, s => s.EmailAddress));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientRenamedFromCustomerRegisteredV1, MigrationsDotnetClientRenamedFromCustomerRegistered> builder) =>
builder.Properties(pb => pb
.RenamedFrom(t => t.EmailAddress, s => s.Email));
}

View C# snippet source on GitHub

Supply a literal value when the source generation has no value for a target property:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType("dotnet-client-task-created", generation: 2)]
public record MigrationsDotnetClientDefaultValueTaskCreated(string Title, string Status, int RetryCount, bool Enabled);
[EventTypeGenerationFor<MigrationsDotnetClientDefaultValueTaskCreated>(1)]
public record MigrationsDotnetClientDefaultValueTaskCreatedV1(string Title);
public class MigrationsDotnetClientDefaultValueTaskCreatedMigration : EventTypeMigration<MigrationsDotnetClientDefaultValueTaskCreated, MigrationsDotnetClientDefaultValueTaskCreatedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientDefaultValueTaskCreated, MigrationsDotnetClientDefaultValueTaskCreatedV1> builder) =>
builder.Properties(pb => pb
.DefaultValue(t => t.Status, "active")
.DefaultValue(t => t.RetryCount, 0)
.DefaultValue(t => t.Enabled, true));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientDefaultValueTaskCreatedV1, MigrationsDotnetClientDefaultValueTaskCreated> builder)
{
// Status, RetryCount, and Enabled did not exist in generation 1 — nothing to map back
}
}

View C# snippet source on GitHub

Object defaults use the client’s JSON naming policy for their members, with [JsonPropertyName] taking precedence. Object values in MapValues follow the same rules so they can match serialized payloads. Scalars and concepts keep their serialized values; enums use their underlying numbers.

If you upgrade from a client that did not apply the naming policy to object literals, the resulting migration content and hash change under camelCase or custom policies. Newly derived content uses the corrected names; already-derived generations are not corrected. Upgrade clients of the same event store together where possible: alternating registrations from old and new clients can replace the definition repeatedly, invalidate its cache and re-subscribe pattern capture. Registering the same definition again is idempotent.

Use a member expression to reach a property inside a nested object. This example adds a description to the event’s Price object:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
public sealed record MigrationsDotnetClientNestedPrice(decimal Amount, string Description);
public sealed record MigrationsDotnetClientNestedPriceV1(decimal Amount);
[EventType("dotnet-client-nested-price-set", generation: 2)]
public record MigrationsDotnetClientNestedPriceSet(MigrationsDotnetClientNestedPrice Price);
[EventTypeGenerationFor<MigrationsDotnetClientNestedPriceSet>(1)]
public record MigrationsDotnetClientNestedPriceSetV1(MigrationsDotnetClientNestedPriceV1 Price);
public class MigrationsDotnetClientNestedPriceSetMigration : EventTypeMigration<MigrationsDotnetClientNestedPriceSet, MigrationsDotnetClientNestedPriceSetV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientNestedPriceSet, MigrationsDotnetClientNestedPriceSetV1> builder) =>
builder.Properties(pb => pb.DefaultValue(m => m.Price.Description, "unspecified"));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientNestedPriceSetV1, MigrationsDotnetClientNestedPriceSet> builder)
{
// Description does not exist in generation 1 - the schema drops it on the way back
}
}

View C# snippet source on GitHub

Chronicle resolves each member in e => e.Price.Description to its JSON name. [JsonPropertyName] takes precedence over the client’s naming policy; without explicit names, camelCase produces price.description. The kernel reads that path through the nested object and creates missing objects when writing the target. The same path handling lets RenamedFrom(m => m.Price.Amount, e => e.Amount) move a top-level value into Price.

Typed source and target expressions use this resolution for every operation, including bidirectional value maps. Raw string paths on IEventMigrationPropertyBuilder are already JSON paths and remain exactly as supplied. Use the serialized names, including any [JsonPropertyName] names: price.description is not converted again under a custom naming policy. Existing raw-string paths need no naming-policy workaround.

Path resolution does not rewrite a separator or a scalar literal, and these builders do not describe arbitrary custom-converter transformations. Object literals follow the serialization rules described under DefaultValue.

Registration checks paths against the generation schemas. An undeclared path is rejected with InvalidMigrationPropertyForEventType, identifying the path to check.

Properties inside the elements of a collection

Section titled “Properties inside the elements of a collection”

Use Select to address a property on every element: DefaultValue(m => m.Days.Select(day => day.Activity), "work") supplies the default where an element has no activity. Under camelCase, the path is days[].activity; raw string paths can use the same form.

The kernel applies the operation to each element and preserves the collection. RenamedFrom and MapValues use the same form when their source paths share the days[]. prefix. A path into a collection without [] is rejected instead of replacing the collection with an object.

When a schema change only adds or removes an optional property, keep a migration class for that generation pair but leave its property operations empty. This applies to nested properties too:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
public sealed record MigrationsDotnetClientOptionalNote(string Text, string? Author = null);
public sealed record MigrationsDotnetClientOptionalNoteV1(string Text);
[EventType("dotnet-client-optional-note-added", generation: 2)]
public record MigrationsDotnetClientNoteAdded(MigrationsDotnetClientOptionalNote Note);
[EventTypeGenerationFor<MigrationsDotnetClientNoteAdded>(1)]
public record MigrationsDotnetClientNoteAddedV1(MigrationsDotnetClientOptionalNoteV1 Note);
public class MigrationsDotnetClientNoteAddedMigration : EventTypeMigration<MigrationsDotnetClientNoteAdded, MigrationsDotnetClientNoteAddedV1>
{
// Author is optional, so the target generation's schema fills it in on the way up
// and drops it on the way down. The class only has to exist.
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientNoteAdded, MigrationsDotnetClientNoteAddedV1> builder)
{
}
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientNoteAddedV1, MigrationsDotnetClientNoteAdded> builder)
{
}
}

View C# snippet source on GitHub

The class completes the generation chain even though there is no value to transform. Use DefaultValue when an absent value is not acceptable. The concept page explains how schema conversion handles unchanged and optional properties; validation still requires a complete chain.

Use a value map when a property keeps its role but its values change, such as renumbered enum members. Override MapValues on the migration to declare a map used in both directions:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
public enum MigrationsDotnetClientPaymentStatusV1
{
Pending = 0,
Settled = 1
}
public enum MigrationsDotnetClientPaymentStatus
{
Awaiting = 10,
Completed = 11
}
[EventType("dotnet-client-payment-processed", generation: 2)]
public record MigrationsDotnetClientPaymentProcessed(MigrationsDotnetClientPaymentStatus Status);
[EventTypeGenerationFor<MigrationsDotnetClientPaymentProcessed>(1)]
public record MigrationsDotnetClientPaymentProcessedV1(MigrationsDotnetClientPaymentStatusV1 Status);
public class MigrationsDotnetClientPaymentProcessedMigration : EventTypeMigration<MigrationsDotnetClientPaymentProcessed, MigrationsDotnetClientPaymentProcessedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientPaymentProcessed, MigrationsDotnetClientPaymentProcessedV1> builder)
{
// Status is covered by the value map
}
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientPaymentProcessedV1, MigrationsDotnetClientPaymentProcessed> builder)
{
// Status is covered by the value map
}
public override void MapValues(IEventValueMapBuilder<MigrationsDotnetClientPaymentProcessed, MigrationsDotnetClientPaymentProcessedV1> builder) =>
builder.For(current => current.Status, previous => previous.Status, map => map
.Map(MigrationsDotnetClientPaymentStatusV1.Pending, MigrationsDotnetClientPaymentStatus.Awaiting)
.Map(MigrationsDotnetClientPaymentStatusV1.Settled, MigrationsDotnetClientPaymentStatus.Completed));
}

View C# snippet source on GitHub

For a translation that is not symmetric (see Value maps in one direction), declare MapValues on the property builder, per direction:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
public enum MigrationsDotnetClientDeclineReasonV1
{
InsufficientFunds = 0,
CardExpired = 1,
CardReported = 2
}
public enum MigrationsDotnetClientDeclineReason
{
Funds = 0,
Card = 1
}
[EventType("dotnet-client-payment-declined", generation: 2)]
public record MigrationsDotnetClientPaymentDeclined(MigrationsDotnetClientDeclineReason Reason);
[EventTypeGenerationFor<MigrationsDotnetClientPaymentDeclined>(1)]
public record MigrationsDotnetClientPaymentDeclinedV1(MigrationsDotnetClientDeclineReasonV1 Reason);
public class MigrationsDotnetClientPaymentDeclinedMigration : EventTypeMigration<MigrationsDotnetClientPaymentDeclined, MigrationsDotnetClientPaymentDeclinedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientPaymentDeclined, MigrationsDotnetClientPaymentDeclinedV1> builder) =>
builder.Properties(pb => pb
.MapValues(current => current.Reason, previous => previous.Reason, map => map
.Map(MigrationsDotnetClientDeclineReasonV1.InsufficientFunds, MigrationsDotnetClientDeclineReason.Funds)
.Map(MigrationsDotnetClientDeclineReasonV1.CardExpired, MigrationsDotnetClientDeclineReason.Card)
.Map(MigrationsDotnetClientDeclineReasonV1.CardReported, MigrationsDotnetClientDeclineReason.Card)));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientPaymentDeclinedV1, MigrationsDotnetClientPaymentDeclined> builder) =>
builder.Properties(pb => pb
.MapValues(previous => previous.Reason, current => current.Reason, map => map
.Map(MigrationsDotnetClientDeclineReason.Funds, MigrationsDotnetClientDeclineReasonV1.InsufficientFunds)
.Map(MigrationsDotnetClientDeclineReason.Card, MigrationsDotnetClientDeclineReasonV1.CardExpired)));
}

View C# snippet source on GitHub

Define one migrator for each adjacent pair. To move from generation 1 to generation 3, Chronicle applies 1→2 followed by 2→3; a direct 1→3 migrator is not a substitute. See Chaining several generations.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType("dotnet-client-multi-gen-person-registered", generation: 3)]
public record MigrationsDotnetClientMultiGenPersonRegistered(string Email, string FirstName, string LastName);
[EventTypeGenerationFor<MigrationsDotnetClientMultiGenPersonRegistered>(2)]
public record MigrationsDotnetClientMultiGenPersonRegisteredV2(string Email, string Name);
[EventTypeGenerationFor<MigrationsDotnetClientMultiGenPersonRegistered>(1)]
public record MigrationsDotnetClientMultiGenPersonRegisteredV1(string EmailAddress, string Name);
// Generation 1 → 2: rename EmailAddress to Email
public class MigrationsDotnetClientMultiGenPersonRegisteredV1ToV2 : EventTypeMigration<MigrationsDotnetClientMultiGenPersonRegisteredV2, MigrationsDotnetClientMultiGenPersonRegisteredV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientMultiGenPersonRegisteredV2, MigrationsDotnetClientMultiGenPersonRegisteredV1> builder) =>
builder.Properties(pb => pb
.RenamedFrom(t => t.Email, s => s.EmailAddress));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientMultiGenPersonRegisteredV1, MigrationsDotnetClientMultiGenPersonRegisteredV2> builder) =>
builder.Properties(pb => pb
.RenamedFrom(t => t.EmailAddress, s => s.Email));
}
// Generation 2 → 3: split Name into FirstName / LastName
public class MigrationsDotnetClientMultiGenPersonRegisteredV2ToV3 : EventTypeMigration<MigrationsDotnetClientMultiGenPersonRegistered, MigrationsDotnetClientMultiGenPersonRegisteredV2>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDotnetClientMultiGenPersonRegistered, MigrationsDotnetClientMultiGenPersonRegisteredV2> builder) =>
builder.Properties(pb => pb
.Split(t => t.FirstName, s => s.Name, PropertySeparator.Space, SplitPartIndex.First)
.Split(t => t.LastName, s => s.Name, PropertySeparator.Space, SplitPartIndex.Second));
public override void Downcast(IEventMigrationBuilder<MigrationsDotnetClientMultiGenPersonRegisteredV2, MigrationsDotnetClientMultiGenPersonRegistered> builder) =>
builder.Properties(pb => pb
.Combine(t => t.Name, PropertySeparator.Space, s => s.FirstName, s => s.LastName));
}

View C# snippet source on GitHub

When your application connects, the .NET client discovers migrators through IClientArtifactsProvider. It invokes Upcast and Downcast to capture their declarations, then sends the generation schemas and declarative migration expressions to the kernel during event type registration.

The kernel applies those definitions on append without invoking your C# migrator again. Registering a new generation also starts the background backfill for existing events in the default namespace’s event log. Registration and backfill completion are separate: an accepted definition does not mean every historical event already has the new generation. See Registration for the shared model.

When generation validation is enabled, the kernel rejects an event type above generation 1 if its migration chain is incomplete. MissingEventTypeMigrators means no migrators were supplied. A chain that has no transition from generation 1 produces MissingFirstGenerationForEventType; a later gap produces MissingMigrationForEventTypeGeneration.

Cratis.Chronicle.Events.Migrations.MissingEventTypeMigrators:
Event type 'AuthorRegistered' is at generation 3 but no migrators are registered for it.

Before deploying, check that every adjacent generation pair has one EventTypeMigration<TUpgrade, TPrevious> subclass and that the old record types remain discoverable. If registration fails, use the reported event type and generation to find the missing transition rather than adding a direct jump to the current generation.