Using Audit.NET.MongoDB with Entity Framework converts Guid Primary keys to binary #665
Replies: 3 comments
-
can you share the code of an entity class with the issue? |
Beta Was this translation helpful? Give feedback.
-
Hello, Thanks for the reply. This is one example: public partial class Tag
.... Thanks |
Beta Was this translation helpful? Give feedback.
-
Please note that applying decorators to entity properties will not be effective since the serialized content under "ColumnValues" and "Changes" represents the property values, not the entity itself. Moreover, I believe this issue is not related to the audit library but rather stems from how the MongoDB driver handles Guids. There are potential workarounds available if you intend to store Guids as Strings, but their feasibility depends greatly on the version of the MongoDB driver you're utilizing. For further insights, refer to: https://stackoverflow.com/questions/13838931/how-can-i-tell-the-mongodb-c-sharp-driver-to-store-all-guids-in-string-format |
Beta Was this translation helpful? Give feedback.
-
I am using Audit.NET.MongoDB with Entity Framework with a simple configuration.
var mongoDataProvider = new MongoDataProvider(config =>
{
config.ConnectionString(mongoConnectionString);
config.Database(auditSettings.DatabaseName);
config.Collection(auditSettings.CollectionName);
config.SerializeAsBson(true);
});
Audit.Core.Configuration.Setup()
.UsePolly(p => p
.DataProvider(mongoDataProvider)
.WithResilience(r => r
.AddFallback(new()
{
ShouldHandle = new PredicateBuilder().Handle(),
OnFallback = arguments =>
{
var auditEvent = arguments.Context.GetAuditEvent();
Log.Information(auditEvent?.ToJson() ?? "Failed to write to audit logs.");
return default;
},
FallbackAction = arguments => default
})));
And also added AuditSaveChangesInterceptor for the DbContext
the issue i am facing is that all primary keys (which are guids) are converted to binary.
"PrimaryKey": {
"Id": {
"$binary": {
"base64": "9FLLoAPE7UOOTOFGo1wM+A==",
"subType": "03"
}
}
How can i ensure that primary keys which are GUIDs are saved as string?
I tried several work arounds including:
BsonSerializer.RegisterSerializer(typeof(Guid), new GuidSerializer(GuidRepresentation.CSharpLegacy));
but nothing worked.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions