Setting up Auditable Entities with EF Core
Setting up Auditable Entities with EF CoreIn this week’s newsletter, I want to introduce you to the concept of auditable entities and how you can implement it with Entity Framework Core.In this week’s newsletter, I want to introduce you to the concept of auditable entities and how you can implement it with Entity Framework Core. For starters, Auditable Entities is a best practice applied on each entity to track changes applied on it. These are nothing but meta columns apart from the attributes, which store information about when the entity was created, updated and the user who did it etc. Maintaining such information helps us keep trail of how these entities have changed over time. However, manually inserting or updating these fields can be painful, as there can be a chance of human error. To automate this process, we can design a system with which we can automatically store such information without having to manually inserting any such value. How do we do that? EF Core exposes a property ChangeTracker which "Provides access to information and operations for entity instances this context is tracking". When we call ChangeTracker.Entities<TClass>() with a type parameter, it returns all the entities currently tracked which are of the type passed. In our case, we passed AuditableEntity which results in all entities which are of type AuditableEntity. Remember that Item extends AuditableEntity so it qualifies the "is-a" relationship. So we get all the Item objects. Since User extends BaseEntity and NOT AuditableEntity, User is not a AuditableEntity and so those entities are not returned. Simple! Within the loop, we see if the record is being added or updated based on the State - EntityState.Added or EntityState.Modified. If it is INSERT we set values for Created and CreatedBy. If UPDATE we set values of LastModified and ModifiedBy. I have explained this in much detail with an illustrating example. The link to the article is here - https://referbruv.com/blog/how-to-setup-audit-fields-using-ef-core/ I hope this article will help you get a better understanding of the concept and help you know something new. I’ll see you next week! |
Older messages
How to Integrate Swagger UI with Spring Boot 3
Wednesday, March 6, 2024
In this week's newsletter, let us see how we can implement SwaggerUI and OpenAPI specification for a simple Spring Boot 3 RESTful API ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
Implementing CORS in an ASP.NET Core Web API
Wednesday, March 6, 2024
In this detailed guide, let's talk about what is CORS and how do we enable CORS in ASP.NET Core with an illustrating example ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
How to use SignalR with .NET 6 and new Digital Product Announcement!
Monday, May 29, 2023
Hello there, Hope you are doing good. This week I wanted to recap on one of the most interesting posts I've ever worked on - Real time communication. How to use SignalR with ASP.NET Core Angular
How to create Social Logins with ASP.NET Core
Monday, May 22, 2023
Hello there, Hope you are doing good. Social Logins are a convenient way of logging in users into system. Users don't need to remember all the logins they have for different applications and
Dockerizing ASP.NET Core Web API and Catching HTTP Errors Globally in Angular
Monday, May 15, 2023
Hello there, Hope you are doing good. For this week's newsletter I want to share with you two articles - One on the client side (Angular) and other on DevOps (Docker). I. How to deploy ASP.NET
You Might Also Like
Stop Using the Wrong State Management in Jetpack Compose
Thursday, November 21, 2024
View in browser 🔖 Articles Benchmark Insights: Direct State Propagation vs. Lambda-based State in Jetpack Compose Here, we'll dive into some benchmark analysis on the state propagation approach in
wpmail.me issue#694
Thursday, November 21, 2024
wpMail.me wpmail.me issue#694 - The weekly WordPress newsletter. No spam, no nonsense. - November 21, 2024 Is this email not displaying correctly? View it in your browser. News & Articles State of
Turn off Google AI with two letters
Thursday, November 21, 2024
$250 off M4 MacBook; Linux Foundation marks 20 years; Bluesky tips -- ZDNET ZDNET Tech Today - US November 21, 2024 laptop This absurdly simple trick turns off AI in your Google Search results There
PHPWeekly November 21st 2024
Thursday, November 21, 2024
Curated news all about PHP. Here's the latest edition Is this email not displaying correctly? View it in your browser. PHP Weekly 21st November 2024 Hi everyone, PHP 8.4 id due for a release today,
Edge 450: Can LLM Sabotage Human Evaluations
Thursday, November 21, 2024
New research from Anthropic provides some interesting ideas in this area. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
Don’t Wait In Line or Online.
Thursday, November 21, 2024
Quick. Easy. Simple. Shop Zugu for the crew. We know we say it every year but, trust us, if feels really good to get ahead of those holiday gifts. Skip the lines, even online, and shop something for
Google's AI-Powered Tool Finds 26 Vulnerabilities in Open-Source Projects
Thursday, November 21, 2024
THN Daily Updates Newsletter cover [Watch LIVE] When Shift Happens: Are You Ready for Rapid Certificate Replacement? Revocations can disrupt your business, but automation saves the day. Discover how.
⚙️ Nvidia doubles revenue
Thursday, November 21, 2024
Plus: US proposes 'Manhattan Project' for AGI
Post from Syncfusion Blogs on 11/21/2024
Thursday, November 21, 2024
New blogs from Syncfusion Secure JWT Storage: Best Practices By Binara Prabhanga Learn about common JWT security risks and best practices for secure JWT storage in SPAs, including HttpOnly cookies,
Top Tech 🏆 The Galaxy Tab S10+ Is Excellent — This is the Most Sturdy Car Phone Holder I've Used
Thursday, November 21, 2024
Also: Testing the The Sennheiser Accentum Earbuds, and More! How-To Geek Logo November 21, 2024 🤖 Android at Its Best The iPad is the most popular tablet, but that doesn't mean other options aren