diff --git a/src/EventSourcingTests/FetchForWriting/fetching_async_aggregates_for_writing.cs b/src/EventSourcingTests/FetchForWriting/fetching_async_aggregates_for_writing.cs index c5dfe56c1e..1b2f928530 100644 --- a/src/EventSourcingTests/FetchForWriting/fetching_async_aggregates_for_writing.cs +++ b/src/EventSourcingTests/FetchForWriting/fetching_async_aggregates_for_writing.cs @@ -498,6 +498,60 @@ public async Task fetch_aggregate_that_is_completely_caught_up() await theSession.SaveChangesAsync(); } + [Fact] + public async Task fetch_aggregate_that_is_completely_caught_up_with_no_version_supplied() + { + StoreOptions(opts => + { + opts.Projections.Snapshot(SnapshotLifecycle.Async); + opts.Events.StreamIdentity = StreamIdentity.AsString; + }); + + var streamId = Guid.NewGuid().ToString(); + + theSession.Events.StartStream(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(), + new CEvent(), new CEvent()); + await theSession.SaveChangesAsync(); + + var daemon = await theStore.BuildProjectionDaemonAsync(); + await daemon.RebuildProjectionAsync(CancellationToken.None); + + var stream = await theSession.Events.FetchForWriting(streamId); + stream.Aggregate.ShouldNotBeNull(); + stream.CurrentVersion.ShouldBe(6); + + stream.AppendOne(new EEvent()); + await theSession.SaveChangesAsync(); + } + + + + [Fact] + public async Task fetch_aggregate_that_is_completely_caught_up_use_exclusive_locks() + { + StoreOptions(opts => + { + opts.Projections.Snapshot(SnapshotLifecycle.Async); + opts.Events.StreamIdentity = StreamIdentity.AsString; + }); + + var streamId = Guid.NewGuid().ToString(); + + theSession.Events.StartStream(streamId, new AEvent(), new BEvent(), new BEvent(), new BEvent(), + new CEvent(), new CEvent()); + await theSession.SaveChangesAsync(); + + var daemon = await theStore.BuildProjectionDaemonAsync(); + await daemon.RebuildProjectionAsync(CancellationToken.None); + + var stream = await theSession.Events.FetchForExclusiveWriting(streamId); + stream.Aggregate.ShouldNotBeNull(); + stream.CurrentVersion.ShouldBe(6); + + stream.AppendOne(new EEvent()); + await theSession.SaveChangesAsync(); + } + [Fact] public async Task fetch_aggregate_that_is_in_progress() { diff --git a/src/Marten/Events/Fetching/FetchAsyncPlan.cs b/src/Marten/Events/Fetching/FetchAsyncPlan.cs index 9468587818..b9df96364f 100644 --- a/src/Marten/Events/Fetching/FetchAsyncPlan.cs +++ b/src/Marten/Events/Fetching/FetchAsyncPlan.cs @@ -147,7 +147,10 @@ public async Task> FetchForWriting(DocumentSessionBase sessio // Read in any events from after the current state of the aggregate await reader.NextResultAsync(cancellation).ConfigureAwait(false); var events = await new ListQueryHandler(null, selector).HandleAsync(reader, session, cancellation).ConfigureAwait(false); - document = await _aggregator.BuildAsync(events, session, document, cancellation).ConfigureAwait(false); + if (events.Any()) + { + document = await _aggregator.BuildAsync(events, session, document, cancellation).ConfigureAwait(false); + } if (document != null) {