EF Core Tracking

net 6 newbie 121 Reputation points
2024-03-13T18:06:23.0033333+00:00

Most of the articles on EF Core suggest don't enable tracking in EF Core.

But if tracking is disabled in EF Core. How to add Audit Logs using EF Core Interceptors?

I have tried this but looks like more re work is required for many cases where tracking is disabled.

Can any one can share a sample code where we can perform Audit Logging when tracking is disabled?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,178 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,136 Reputation points
    2024-03-13T20:06:42.01+00:00

    Most of the articles on EF Core suggest don't enable tracking in EF Core.

    You'll find tracking being turned off in web application tutorials due to the stateless nature of web applications. If Web API or a service returns a result set then there is no reason to track changes because the DbContext is torn down once the response completes. The official documentation covers how tracking works.

    https://learn.microsoft.com/en-us/ef/core/querying/tracking

    But if tracking is disabled in EF Core. How to add Audit Logs using EF Core Interceptors?

    If you are have a complex operation that requires EF Core Interceptors and/or you need tracking then do not disable tracking for the complex operation.

    Can any one can share a sample code where we can perform Audit Logging when tracking is disabled?

    I think if you are mindful of how tracking works, you find this is a nonissue. If you have a specific problem, please share the code. Explain how you expect the code to function and what actually happens.

    0 comments No comments