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
Weekend Reading — Happy "That's a January Problem" week
Saturday, December 21, 2024
Can Christmas season start a little earlier this year Tech Stuff Ramsey Nasser fuck it happened i am in a situation where i do actually need to reverse a linked list Atuin I just learned about Atuin
Daily Coding Problem: Problem #1644 [Easy]
Saturday, December 21, 2024
Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by IBM. Given an integer, find the next permutation of it in absolute order. For example,
🐧 Whatever Happened to Unix Workstations? — My Incredibly Cheap Alternative to a Soundbar
Saturday, December 21, 2024
Also: Here's Why More Games Need Expanded Difficulty Settings How-To Geek Logo December 21, 2024 Did You Know Lake Wendouree, an artificially created and maintained shallow urban lake in Australia,
Supercharge Your Knowledge Capture Workflow with the Obsidian Web Clipper
Saturday, December 21, 2024
Stop juggling multiple tools and supercharge your knowledge capture workflow with Obsidian's powerful Web Clipper browser extension Sébastien Dubois DeveloPassion's Newsletter Supercharge Your
Charted | The World's Most Valuable Automakers 🚙
Saturday, December 21, 2024
Tesla shares reached a record high, setting a new valuation milestone. This graphic highlights the world's most valuable automakers by market cap. View Online | Subscribe | Download Our App
Next Holiday Season, Ignore Everyone Except One Customer
Saturday, December 21, 2024
Top Tech Content sent at Noon! Boost Your Article on HackerNoon for $159.99! Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, December 21, 2024? The
🐍 New Python tutorials on Real Python
Saturday, December 21, 2024
Hey there, There's always something going on over at Real Python as far as Python tutorials go. Here's what you may have missed this past week: 🎓 Master Python's Core Principles (New Live
Post from Syncfusion Blogs on 12/21/2024
Saturday, December 21, 2024
New blogs from Syncfusion ASP.NET MVC Suite Update: Aligning with .NET Changes By Rajendran R Discover key updates in our ASP.NET MVC suite, aligning with Microsoft's latest .NET changes for
LockBit Ransomware Developer Charged for Billions in Global Damages
Saturday, December 21, 2024
THN Daily Updates Newsletter cover The Data Science Handbook, 2nd Edition ($60.00 Value) FREE for a Limited Time Practical, accessible guide to becoming a data scientist, updated to include the latest
Re: My VPN recommendation
Saturday, December 21, 2024
Do you know when to use a VPN and what it does to protect your data? Any time you are connected to the internet, your information is at risk of being tracked or hacked. A VPN helps keep your surfing