-
-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EF Core value updates are not audited #607
Comments
When using SaveChanges override method without inheritance, you have to make sure you are overriding the following two methods in your DbContext: int SaveChanges(bool acceptAllChangesOnSuccess) Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken)) Just so you know, no other SaveChanges override is needed, since the other overloads will call one of these two. It looks like you are not overriding the first one, so maybe the client that is not triggering the audit is calling the Another option is to use a SaveChanges interceptor instead of overriding the SaveChanges: |
Are you calling Can you share a minimal solution that reproduces the issue? |
It seems I found out why Audit events were not being triggered I am using the following method to Bulk-Update am Entity list
When saving in the database this way, Audit events are not getting triggered. But if I add the following in the method body,
The audit event is triggered.
How can I make it so that doing Bulk-Updates also trigger the Audit Events. |
The Bulk updates and deletes using Check this: https://learn.microsoft.com/en-us/ef/core/saving/execute-insert-update-delete#change-tracking The only way would be to add the AuditCommandInterceptor, which will generate logs like: {
"CommandEvent": {
"Method": 0,
"CommandType": 1,
"CommandText": "DELETE TOP(@__p_0) FROM [v]\r\nFROM [Values] AS [v]",
"Result": 1,
"Database": "Test_1",
"ConnectionId": "3001fe63-b822-4422-b59e-7b941a05b2ad",
"DbConnectionId": "33e15718-7c07-45d8-953c-acea98a0a0a8",
"ContextId": "b72a194f-7b66-4fe2-8a4d-5e178d0edb7d:0",
"IsAsync": false,
"Success": true
},
"EventType": "EF",
... |
I am not getting what changed in the Logs The generated JSON looks like this: {"commandEvent":{"method":2, "commandType":1, "commandText": The command text has a "SELECT" statement after performing BulkUpdate. Ive placed the AuditCommandInterceptor config inside DbContext configuration: protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
I didn't find any official documentation stating so, but I think BulkUpdate cannot be intercepted in any way. https://www.milanjovanovic.tech/blog/how-to-use-the-new-bulk-update-feature-in-ef-core-7 |
Hello! I am facing another issue here My Updating method looks like this:
I called this method like this :
here i passed the list of updated patients which i got by creating a new instance of the MyContext like this: MyContext _db = new MyContext(connectionString); and then modifying a fey patient entities. Why am I getting the old value and new value exactly the same even when there is an update. Is there a way I get the correct change records? The Audit Json looks like this
|
Doing the update with Update / UpdateRange will not retrieve the old values from the database, that's the way EF Change Tracker works. From EF documentation here "Note that whenever real original property values are not available (e.g. entity was not yet persisted to the database) this will default to the current property values of this entity." Please check this: #53 (comment) |
I'm thinking that the Audit.EF library could provide an optional mechanism to load the original values before any modification, to cover the use of Update / UpdateRange (and also Remove and manually attached entities). This mechanism could use the GetDatabaseValues() function. Will take a deeper look |
Starting from version 21.0.2, the Audit.EntityFramework and Audit.EntityFramework.Core libraries introduce a new feature. The setting called Consider the following examples of update and delete operations: using (var context = new MyAuditedContext())
{
//context.ReloadDatabaseValues = true;
context.Cars.Update(new Car() { Id = 123, Name = "New name" });
await context.SaveChangesAsync();
} using (var context = new MyAuditedContext())
{
//context.ReloadDatabaseValues = true;
context.Entry(new Car() { Id = 123 }).State = EntityState.Deleted;
await context.SaveChangesAsync();
} When doing modifications like these, the EF Change Tracker will lack knowledge of the original values. Enabling the |
Hello!
and set the ReloadDatabaseValues when creating the db context
But this is not working when I try and configure it Globally for the DbContext
|
You could do that, but you should call public MyContext(string connectionString) : base(GetOptions(connectionString))
{
_auditContext = new DefaultAuditContext(this);
_helper.SetConfig(_auditContext);
_auditContext.ReloadDatabaseValues = true;
} But the recommended way to set the configuration globally is by using the fluent API, for example: Audit.EntityFramework.Configuration.Setup()
.ForAnyContext(cfg => cfg.ReloadDatabaseValues()); Or: Audit.EntityFramework.Configuration.Setup()
.ForContext<MyContext>(cfg => cfg.ReloadDatabaseValues()); Also, I've just realized I didn't add a property to the So, you will be able to set the configuration like this: [AuditDbContext(ReloadDatabaseValues = true, Mode = AuditOptionMode.OptIn, IncludeEntityObjects = false, AuditEventType = "EF")]
public class MyContext : DbContext
{
...
} |
EntityFramework changes are not getting audited or detected.
I am using a dotnet project as a package to make any DB changes.
ie the package houses the databaseContext (Persistence package)
I use another .net project to trigger these database changes through repository methods.
The Persistence package is used by multiple projects.
The database changes through this package are properly audited when running from other .net projects, but completely ignored when using from one .net project.
The config I set up for both these projects is the same. Yet Audits are not triggered from this particular project.
My setup:
Persistence package (Common .net project that houses the databaseContext and is used as a package)
In the DBContext Class:
`
[AuditDbContext(Mode = AuditOptionMode.OptIn, IncludeEntityObjects = false, AuditEventType = "EF")]
public class CWContext : DbContext
{
private readonly DbContextHelper _helper = new DbContextHelper();
private readonly IAuditDbContext _auditContext;
`
In the Entity class:
[AuditInclude]
public class Patient
{
. . .
CSPROJ:
net6.0
. . .
In the .net Project that uses this Persistence package
(Project A-NotWorking)
In the ProjectStartup code:
}
The AuditLogsSetup static class:
(Moving the contents of the static method directly up to the startup code didn't work either)
public static class AuditLogsSetup
{
////////////////////// I am using a custom data provider to write logs into ES from kinesis firehose here in other projects, but here, the default File Provider is also not working /////////////////////////////////
public static void SetupAuditConfigs()
{
Audit.Core.Configuration.Setup()
.UseFileLogProvider(_ => _
.Directory(@"C:\Logs\AuditLogs")
.FilenameBuilder(ev => $"{ev.StartDate:yyyyMMddHHmmssffff}_{ev.EventType}.json"));
}
}
CSPROJ:
Am I missing configuring something here?
The text was updated successfully, but these errors were encountered: