user cannot be converted to type system.string
Time to time I have a runtime exception about converting to system.string:

where ApplicationUser is the next class:
public class ApplicationUser : IdentityUser
I can not find any particular steps to reproduce, it just occurs time to time.
I use: Audit.NET 19.0.1 Audit.NET.MySql 19.0.1 Audit.EntityFramework.Core 19.0.1
with .NET 6
and the next confugiration: `Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope => { scope.SetCustomField("AuditUser", ctxAccessor.HttpContext?.User?.Identity?.Name ?? "guest"); });
Audit.Core.Configuration.Setup()
.UseEntityFramework(x => x
.AuditTypeMapper(typeName => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, ent, auditEntity) =>
{
auditEntity.User = ev.CustomFields["AuditUser"]?.ToString() ?? "guest";
auditEntity.Table = ent.Table;
auditEntity.AuditDate = DateTime.UtcNow;
auditEntity.Action = ent.Action;
auditEntity.Changes = JsonSerializer.Serialize(ent.Changes);
auditEntity.Entries = JsonSerializer.Serialize(ev.GetEntityFrameworkEvent().Entries);
auditEntity.Success = ev.GetEntityFrameworkEvent().Success;
auditEntity.ColumnValues = JsonSerializer.Serialize(ent.ColumnValues);
auditEntity.PrimaryKey = JsonSerializer.Serialize(ent.PrimaryKey);
}));`
Any ideas how can I fix this issue?
I think you're missing the IgnoreMatchedProperties(true) configuration
Audit.Core.Configuration.Setup()
.UseEntityFramework(x => x
.AuditTypeMapper(typeName => typeof(AuditLog))
.AuditEntityAction<AuditLog>((ev, ent, auditEntity) =>
{
auditEntity.User = ev.CustomFields["AuditUser"]?.ToString() ?? "guest";
auditEntity.Table = ent.Table;
auditEntity.AuditDate = DateTime.UtcNow;
auditEntity.Action = ent.Action;
auditEntity.Changes = JsonSerializer.Serialize(ent.Changes);
auditEntity.Entries = JsonSerializer.Serialize(ev.GetEntityFrameworkEvent().Entries);
auditEntity.Success = ev.GetEntityFrameworkEvent().Success;
auditEntity.ColumnValues = JsonSerializer.Serialize(ent.ColumnValues);
auditEntity.PrimaryKey = JsonSerializer.Serialize(ent.PrimaryKey);
}).IgnoreMatchedProperties());
Otherwise, the framework will try to match the Entity properties to the AuditLog properties automatically
didn't get any complaints recently, so it seems that this config helped. Thank you.
I guess some of your entity classes have a property called User of type ApplicationUser, and the property matching mechanism was trying to set the ApplicationUser object to the AuditLog.User string property, and failed