diff --git a/src/EventSourcingTests/Aggregation/blue_green_deployment_of_aggregates.cs b/src/EventSourcingTests/Aggregation/blue_green_deployment_of_aggregates.cs index 17125041f6..7a4810b3e0 100644 --- a/src/EventSourcingTests/Aggregation/blue_green_deployment_of_aggregates.cs +++ b/src/EventSourcingTests/Aggregation/blue_green_deployment_of_aggregates.cs @@ -38,6 +38,20 @@ public void apply_the_version_suffix_to_table_alias_when_version_attribute_is_on var mapping = store.Options.Storage.MappingFor(typeof(OtherAggregate)); mapping.Alias.ShouldBe("otheraggregate_3"); } + + [Fact] + public void apply_the_version_suffix_when_document_alias_is_set_via_fluent_api() + { + using var store = DocumentStore.For(opts => + { + opts.Connection(ConnectionSource.ConnectionString); + opts.Projections.Add(ProjectionLifecycle.Async); + opts.Schema.For().DocumentAlias("custom_alias"); + }); + + var mapping = store.Options.Storage.MappingFor(typeof(MyAggregate)); + mapping.Alias.ShouldBe("custom_alias_2"); + } } public class Version2: SingleStreamProjection diff --git a/src/Marten/Events/Projections/ProjectionDocumentPolicy.cs b/src/Marten/Events/Projections/ProjectionDocumentPolicy.cs index bd5d0621ad..30b82a31e2 100644 --- a/src/Marten/Events/Projections/ProjectionDocumentPolicy.cs +++ b/src/Marten/Events/Projections/ProjectionDocumentPolicy.cs @@ -14,11 +14,6 @@ public void Apply(DocumentMapping mapping) if (mapping.StoreOptions.Projections.TryFindAggregate(mapping.DocumentType, out var projection)) { - if (projection.Version > 1) - { - mapping.Alias += "_" + projection.Version; - } - mapping.UseOptimisticConcurrency = false; mapping.Metadata.Version.Enabled = false; mapping.UseNumericRevisions = true; @@ -28,7 +23,26 @@ public void Apply(DocumentMapping mapping) { m.ConfigureAggregateMapping(mapping, mapping.StoreOptions); } + } + } +} +internal class ProjectionVersionAliasPolicy : IDocumentPolicy +{ + public void Apply(DocumentMapping mapping) + { + if (mapping.StoreOptions.Projections == null) return; + + if (mapping.StoreOptions.Projections.TryFindAggregate(mapping.DocumentType, out var projection)) + { + if (projection.Version > 1) + { + var suffix = "_" + projection.Version; + if (!mapping.Alias.EndsWith(suffix)) + { + mapping.Alias += suffix; + } + } } } } diff --git a/src/Marten/StoreOptions.cs b/src/Marten/StoreOptions.cs index eeb6e9890e..42c5bda244 100644 --- a/src/Marten/StoreOptions.cs +++ b/src/Marten/StoreOptions.cs @@ -72,7 +72,7 @@ internal INpgsqlDataSourceFactory NpgsqlDataSourceFactory new ProjectionDocumentPolicy() ]; - private readonly List _postPolicies = new(); + private readonly List _postPolicies = new() { new ProjectionVersionAliasPolicy() }; /// /// Register "initial data loads" that will be applied to the DocumentStore when it is