-
Pasting from discussion here hoping for an answer 😄 Hi, I have a scenario where we get an event from an external system, this event is mapped and written to a marten event store for auditing. I have enabled the unique key index on the ID column and would like to manually set the ID on my event records. I think from the docs it just asks to set an property such as https://github.com/mumby0168/marten-id-demo for ease of viewing here is the code snippet: using System.Text.Json;
using System.Text.Json.Serialization;
using JasperFx.Core;
using Marten;
using Marten.Events;
using Marten.Services;
using MartenIdDemo.Events;
using Weasel.Core;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMarten(
martenOptions =>
{
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Please provide a connection string to Postgres via ConnectionStrings:DefaultConnection");
martenOptions.Connection(connectionString);
martenOptions.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
martenOptions.Events.EnableUniqueIndexOnEventId = true;
martenOptions.Events.StreamIdentity = StreamIdentity.AsString;
martenOptions.OpenTelemetry.TrackEventCounters();
martenOptions.OpenTelemetry.TrackConnections = TrackLevel.Normal;
martenOptions.UseSystemTextJsonForSerialization(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() }
});
});
var app = builder.Build();
var docStore = app.Services.GetRequiredService<IDocumentStore>();
await docStore.Storage.ApplyAllConfiguredChangesToDatabaseAsync();
app.MapGet("/", async (IDocumentStore documentStore) =>
{
var id = Guid.NewGuid();
var evt = new UserEvents.SignedUp(id, Guid.NewGuid().ToString());
await using (var session = documentStore.LightweightSession())
{
session.Events.Append(evt.UserId, evt);
await session.SaveChangesAsync();
}
await using (var session = documentStore.LightweightSession())
{
var returnedEvt = await session
.Events
.QueryAllRawEvents()
.Where(x => x.Id == id)
.ToListAsync();
return Results.Ok(returnedEvt);
}
});
app.Run(); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@mumby0168 Next time could you use either discussions or better yet, Discord for questions? Here: https://discord.gg/cFy5Xctb When you all |
Beta Was this translation helpful? Give feedback.
Thanks @jeremydmiller I just created this small extension method and it's working as I'd hoped :)
how do you feel about either this or something similar been contributed to the library?