Auditing Your C# Models with Entity Framework

Auditing Your C# Models with Entity Framework

When working with data, oftentimes my clients or customers have asked me, “who changed that record?”. I always add some audit info to all my entities (models/tables) to know who added or changed a piece of data and when it was last changed.

Image for post

When working with data, oftentimes my clients or customers have asked me, “who changed that record?”. I always add some audit info to all my entities (models/tables) to know who added or changed a piece of data and when it was last changed.

Let’s start by creating an interface called IEntity that demands that my auditable entities will need to have the following fields:

  • Id — this is my primary key. I tend to keep my primary key in the base entity because all entities will invariably have a primary key (even many-to-many relationship objects, but make sure to define your composite key in the DbContext). By convention, Entity Framework recognizes a property named Id or Id as the primary key of an entity.
  • CreatedBy — a property of type string that defines who created this record.
  • CreatedOnUtc — a property of type _DateTime _that defines when this record was created.
  • LastModifiedBy — a property of type _string _that defines who last updated this record.
  • LastModifiedOnUtc — a property of nullable type _DateTime? _that defines when this record was last updated.
  • IPAddress — a property of type _string _that stores the current user’s IP Address, though this for me lies in a moral grey area. I simply want to show here how this is technically implemented.
  • IsDeleted — a property of type _bool _to enable soft deletes on your entity. You can omit this property if your business case does not call for it.
public interface IEntity
    object Id { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedOnUtc { get; set; }
    public string LastModifiedBy { get; set; }
    public DateTime? LastModifiedOnUtc { get; set; }
    public string IPAddress { get; set; }
    public bool IsDeleted { get; set; }

Then create a BaseEntity class that implements IEntity. The base entity is marked as an abstract class because you will never need to instantiate it. It simply serves as a base class for your actual entities.

development programming software-engineering c-sharp-programming software c-sharp

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Software Developer vs Software Engineer — Differences: Bogus or Real?

In this article, see if there are any differences between software developers and software engineers. What you’re about to read mostly revolves around my personal thoughts, deductions, and offbeat imagination. If you have different sentiments, add them in the comment section, and let’s dispute! So, today’s topic…

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Best C# (C-Sharp) Programming Courses for Beginners in 2020

In this article, I am going to share some of the best courses to learn C# or C-Sharp in 2020. These courses are suitable for both beginners and experienced programmers who want to fill gaps in their C# knowledge.

Developer or Engineer? Does It Make a Difference?

To summarise the main differences between the software developer and engineer: A developer executes. ... So the software developer is mainly focused on developing code that is a part of software development cycle. An engineer designs and plans applying the principles of engineering to software development.

C programming for Beginners | Learn C Programming | C Language

C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.