Skip to content

Commit 9f32ece

Browse files
committed
Fixed the erroneous usage of aggregate id members because it was not using the "official" mapping. Closes GH-3851
1 parent cc63c56 commit 9f32ece

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Marten.Testing.Harness;
4+
using Shouldly;
5+
using Xunit;
6+
7+
namespace EventSourcingTests.Bugs;
8+
9+
public class Bug_3851_should_use_document_mapping_from_store_options_for_id_member : BugIntegrationContext
10+
{
11+
[Fact]
12+
public async Task aggregate_correctly()
13+
{
14+
StoreOptions(opts =>
15+
{
16+
opts.Schema.For<EntityWithoutConventionalId>().Identity(x => x.OtherId);
17+
18+
});
19+
20+
var streamId = Guid.NewGuid();
21+
22+
theSession.Events.StartStream<EntityWithoutConventionalId>(streamId, new EntityCreated("one"),
23+
new ContentAdded("two"));
24+
25+
await theSession.SaveChangesAsync();
26+
27+
var entity = await theSession.Events.AggregateStreamAsync<EntityWithoutConventionalId>(streamId);
28+
29+
entity.InitialContent.ShouldBe("one");
30+
entity.MoreContent.ShouldBe("two");
31+
}
32+
}
33+
34+
public class EntityWithoutConventionalId
35+
{
36+
public Guid OtherId { get; set; }
37+
public string? InitialContent { get; set; }
38+
public string? MoreContent { get; set; }
39+
40+
public void Apply(EntityCreated @event)
41+
{
42+
InitialContent = @event.Content;
43+
}
44+
45+
public void Apply(ContentAdded @event)
46+
{
47+
MoreContent = @event.Content;
48+
}
49+
}
50+
51+
public record EntityCreated(string Content);
52+
public record ContentAdded(string Content);

src/Marten/Events/EventGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal EventGraph(StoreOptions options)
8787

8888
public IAggregatorSource<IQuerySession>? Build<TDoc>()
8989
{
90-
var idType = new DocumentMapping(typeof(TDoc), Options).IdType;
90+
var idType = Options.Storage.MappingFor(typeof(TDoc)).IdType;
9191
return typeof(SingleStreamProjection<,>)
9292
.CloseAndBuildAs<IAggregatorSource<IQuerySession>>(typeof(TDoc), idType);
9393
}

0 commit comments

Comments
 (0)