Closed asadsahi closed 2 years ago
dont you need an await?
Where?
await SaveChangesAsync(); and so on
@asadsahi Have you verified that your SaveChanges method is being called? If it is not being called, then check:
@ajcvickers Save changes method was being called, but for some reason it wasn't auditing entities. Looking at the code it sounds a little strage, but not sure why it was doing it. After changing to non-async version of controller actions and calling savechanges synchronously too, shadow properties were being updated.
However, got it working now with changing savechange to async as well.
So what works for me is overriding SaveChange to SaveChangesAsync and changing controller actions to async too.
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
this.AuditEntities();
return base.SaveChangesAsync(cancellationToken);
}
What doesn't work is calling savechanges synchronously from async controller actions.
I am using IdentityDbContext instead of custom one.
@asadsahi When you say, "Save changes method was being called, but for some reason it wasn't auditing entities." can you explain a bit more what you mean by "it wasn't auditing entities?" Which part of the AuditEntities method was not working? For example, was ChangeTracker.Entries<IAuditable>()
returning no values? Or something else?
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
I've had the same "issue". Overriding SaveChanges/SaveChangesAsync is confusing for me. @ajcvickers, you are absolutely right that the function called should be the same that overridden :)
But, looking into code I see FOUR SaveChanges.
int SaveChanges()
int SaveChanges(bool acceptAllChangesOnSuccess)
Task
Which of these should I override? Naive way for me was to override all of them but I've found that they call each other.
I would prefer to have single method to override :)
@andrzejmartyna EF never calls SaveChanges, so really you only need to overrride the method(s) that your code will call. That being said, there are async and sync methods which don't call each other. Within each group one method is just sugar for calling the other method with a default value for the param. So overriding the one with the param would catch all uses.
I am auditing all my entities using 4 extra shadow properties by implementing IAuditable interface to tell EF to log only those entities which implement IAuditable interface:
Here is my DB context class:
From Asp.Net async controller action when I save changes shadow properties aren't logged:
However, synchronous version of action calls save changes and logs the shadow properties correctly.
Am I missing something in context or during save changes?
Thanks