[Question] Json data using ef core
See original GitHub issueFirst of all, I may be wrong, sorry in advance. I want to use web api audit and ef core audit. I’m using a customized “provider” as far as I can see from the examples. I learned that “web api audit” will not be used with “ef core audit” and I switched to “sql data provider”. I am currently using ef core by default in my project.
public class MyCustomDataProvider : AuditDataProvider
{
private readonly SqlDataProvider _webApiEventsDataProvider;
private readonly SqlDataProvider _efEventsDataProvider;
public MyCustomDataProvider(Action<ISqlServerProviderConfigurator> webApiConfig,
Action<ISqlServerProviderConfigurator> efConfig)
{
_webApiEventsDataProvider = new SqlDataProvider(webApiConfig);
_efEventsDataProvider = new SqlDataProvider(efConfig);
}
public override object InsertEvent(AuditEvent auditEvent)
{
return GetDataProvider(auditEvent).InsertEvent(auditEvent);
}
public override async Task<object> InsertEventAsync(AuditEvent auditEvent)
{
return await GetDataProvider(auditEvent).InsertEventAsync(auditEvent);
}
private SqlDataProvider GetDataProvider(AuditEvent auditEvent)
{
return auditEvent switch
{
AuditEventWebApi _ => _webApiEventsDataProvider,
AuditEventEntityFramework _ => _efEventsDataProvider,
_ => throw new ArgumentException("Audit.Net provider is null")
};
}
}
The first question I want to ask now is;
Since I use ef core by default, does it make sense for me to use a different “provider” again? How to use web api audit with ef core audit?
public static void UseSettingsAudit(this IApplicationBuilder app, IConfiguration configuration)
{
string connectionString = configuration.GetConnectionString("DefaultConnection");
_ = Audit.Core.Configuration.Setup()
.UseCustomProvider(new MyCustomDataProvider(api => api.ConnectionString(connectionString)
.Schema("dbo")
.TableName("MyAuditWebApi")
.IdColumnName("Id")
.JsonColumnName("AuditData")
.CustomColumn("AuditDate", _ => DateTime.Now),
ef => ef.ConnectionString(connectionString)
.Schema("dbo")
.TableName("MyAuditEFCore")
.IdColumnName("Id")
.CustomColumn("TableName", auditEvent => auditEvent.GetEntityFrameworkEvent().Entries[0].Table)
.CustomColumn("TableId",
ev => Convert.ToInt32(ev.GetEntityFrameworkEvent().Entries[0].PrimaryKey.FirstOrDefault()
.Value))
.CustomColumn("UserName", auditEvent => "DOLDURULACAK")
.CustomColumn("AuditData", auditEvent =>
{
var entries = auditEvent.GetEntityFrameworkEvent().Entries[0];
object data = entries.Action is "Update" ? entries.Changes : entries.ColumnValues;
return JsonSerializer.Serialize(data, new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
})
.CustomColumn("Action", ev => ev.GetEntityFrameworkEvent().Entries[0].Action)
.CustomColumn("AuditDate", e => DateTime.Now)));
}
I’m getting “connectionstring” again, transferring it to the provider or something, it didn’t make sense to me. I already use ef core by default.
my second question;
you’ve probably seen it. With .net 7, the json column feature came. As far as I understand from the documents, a json column query is created over the classes. to do this i need to create classes for my model. Of course, how do I query the dynamically saved data with constants, there is a little problem there.
https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew
By the way your libraries are very good. thank you for everything. I guess there is no problem with saving, there is a problem with queries, at least for me.
Issue Analytics
- State:
- Created 7 months ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
@mansurdegirmenci Note that the overload with the guard condition for Custom Columns in SqlServer data provider was added in version 20.2.0
You can add a parameterless constructor to the entity classes (CustomJsonEntry, CustomJsonEntryChange) it shouldn’t be a problem. the custom constructors were only for making the configuration shorter.
For the DbContext constructor issue, I think you have some options:
.UseDbContext()
call, so the same DbContext instance will be used to store the audits.or
.UseDbContext(Func<AuditEventEntityFramework, DbContext>)
that let you create the DbContext instance for each event, for example using the HttpContextAccessor to get the DbContext from the service provider:or