Unclear how I can reliably set the Partition Key in DynamoDB in my EF repository layer #613
Replies: 2 comments 15 replies
-
If you added the EF interceptor and surrounded the save changes with an If so, you'll need something to co-relate the events. For example, if you're in an ASP.NET Core application, you could use the request trace identifier. I think the easiest way to attach the TraceIdentifier to the event is via a Custom Action in your startup logic: public void ConfigureServices(IServiceCollection services)
{
services.AddHttpContextAccessor();
...
}
public void Configure(IApplicationBuilder app, IHttpContextAccessor contextAccessor)
{
Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var httpContext = contextAccessor.HttpContext;
scope.Event.CustomFields["CorrelationId"] = httpContext.TraceIdentifier;
});
...
} Now all your events will have the "CorrelationId" custom field set to a unique identifier by request |
Beta Was this translation helpful? Give feedback.
-
Another option is to use the DbContext.ContextId as a custom field to correlate the events, you'll need to change the order of the using (NoteContext context = _ContextFactory.GetContext<NoteContext>())
using (AuditScope scope = AuditScope.Create(new AuditScopeOptions { ExtraFields = new { EntityId = noteId, ContextId = context.ContextId.ToString() } }))
{
... So now the manual event will also include the ContextId property (EF events already includes the context id by default) which is unique per DbContext instance |
Beta Was this translation helpful? Give feedback.
-
I'm using Audit.Net for all of our audits and have it turned on to audit every create or update event using the Entity Framework interceptor. We're pushing our events to DynamoDB. Up until now we've set the Partition Key to a random UUID for every event written, but this is not a great option as we can't look up all changes to a particular object. For example, if we want to see all of the events against the Orders table for order with OrderId 123. For this reason we're wanting to change the Partition Key to always be the primary key value of the table we're auditing.
It's not clear to me how I can do this in a thread safe way that supports all tables. I think I can do something like this in a basic operation
I think that will cause it to write the
noteId
value into theEntityId
field and I think that would be thread safe. Is that right? If so, then, my next wonder would be what would happen if I am updating more than one table in a single block like that? For instance if I'm updating theNote
table and theOrder
table? How would the scope know when to putNoteId
in theEntityId
field and when to putOrderId
in theEntityId
field?I'm thinking there must be some way to achieve what I'm trying to achieve more easily though. For example, if there was a global configuration I could do like this?
Thanks for the help on learning what my options are here.
Beta Was this translation helpful? Give feedback.
All reactions