How To Track Entity Changes With EF Core | Audit Logging

preview_player
Показать описание

Tracking entity changes can be cumbersome to implement. However, with EF Core there is an easy way to implement entity change tracking as a cross-cutting concern. In this, I'll show you how you can track changes on your entity, write the code for tracking changes once, and never have to worry about it again.

3 Ways To Create Middleware In ASP.NET Core

Join my weekly .NET newsletter:

Subscribe for more:

Chapters
0:00 What is Auditing?
0:30 IAuditableEntity
2:30 Using EF interceptors for auditing
6:09 Configuring the interceptor
7:27 Auditing in action
Рекомендации по теме
Комментарии
Автор

a quick reminder from the case I encountered, there are two methods looks very similar in SaveChangesInterceptor, one is called SavingChangesAsync and one is call SavedChangesAsync, in thevideo we are implementing SavingChangesAsync. sometimes your IDE will aurocomplete to the other one. make sure you are overriding the correct method otherwise it won't work

williamliao
Автор

4:30 Was that some sort of shortcut you made there to create the foreach that fast? - Or was it the editing of the video that made it look like magic?

CRBarchager
Автор

Did you share the full code of this project ? I want to learn clean arc. From it

androidsavior
Автор

This is populating some audit fields but you still only have the latest state of the entity. There is no capture here of the values that were changed. Yes, I know when a change was made, and you can also include the user to know who changed it. But you don't know what was changed. This may be fine for some use cases.

One option to get a real audit log with changes is to use TemporalTables if you are in SQL server.

Or, if you really want a more robust method take a look at the EventSourcing pattern.

pilotboba
Автор

Thank you for the tutorial, very useful.

kodindoyannick
Автор

Do you have a video on how you set up your visual studio? I love the way your Intellisense looks, and your color scheme. It looks like default with just a few nice modifications.

thematthewyoung
Автор

I see, seems interesting. Currently I am overriding the savechangesasync method but it has some issues in my use cases. This seems to resolve those definately will give it try :) Thanks :)

bilalmehrban
Автор

Hey Milan, great video. I have already implemented this kind of audit using EFCore, but I have found one setback in this. If you have related entities in your model, and for some reason it happens that the related entities are modified, then this way of audit will not work because EFCore change tracker doesn't track related entities (as per my experience). Have you found a workaround on this ?

shkelqimhaxha
Автор

Opinions on using this approach vs having DB triggers for Insert/Update/Delete operations?

mladenstankovic
Автор

Thanks, nice video. I think VS2022 has also the possibility to cleanup unused namespaces on file save

jolambrichts
Автор

Thank you for the tutorial, very useful.
There is something that I have noticed: the State is always Unchanged in the after change listener. that feels like a very important oversight given it will never tell you what happened.
There is always also the issue that EF Core does not have events for delete. 🤦‍♂

vprix
Автор

Can the interceptor have a Principal in orher to obtain curren claims of user?

gerarduab
Автор

Milan, thanks for the awesome videos. What if you wanted to track the table columns affected, their previous values before CRUD and the new values after CRUD?

emwagner
Автор

Milan, is it possible to capture here the ID of the person who made the modification or creation to save not only the date but also who made the transaction?

fernandocalmet
Автор

Nice feature indeed !
I am wondering, what are pros and cons of Auditing
using Temporal tables in SQL Server vs Auditing using Interceptors ?
Thanks Milan

KenzoArts
Автор

There is 'Entity' property on EntityEntry through which you can access properties directly. Also the described method won't work if we want to keep track of IDs generated by the database on insert.

elpe
Автор

How would you update in this approach if the interface has a user update field?

anonim
Автор

How do you recommend to pass logged in user id to log who added/ changed the record ?

microtech
Автор

Is It possible to recaive link to github for that code?

kondziossj
Автор

Can you share a sample project with good DDD/CQRS pratices?

kuba