How to easily add Auditing to an Entity Framework Code-First project

In a database driven application, one of the core requirements among others is database auditing. Stakeholders always want to know who created or effected changes to a record and when such action was taken. Developers in most cases seldom consider auditing at the early stage of an application development, which later becomes a bottle-neck trying to refactor this aspect into a project.

This article will focus on how to easily implement auditing in an ASP.NET MVC project using Entity Framework and Dev Tools DataKit Framework.

Setup

I will be authoring an mvc project from scratch as you follow along, but if you have an existing project and just want to get into the nitty-gritty, just skip the project creation part and go straight to the audit implementation part. In this article, I'll be using C# however, the codes can be easily ported to VB.NET.

Project

Let's start by creating an ASP.NET Web Application using the MVC template.

In the "New Project" dialog, choose "Web" under installed and choose "ASP.NET Web Application (.NET Framework)" from the enumerated templates. Enter a project name and choose "OK".



In the "New ASP.NET Web Application" dialog, choose "MVC". Change "Authentication" type to "No Authentication" and choose "OK".



In a short moment, our project would be created.


Next, we are going to install Dev Tools DataKit Framework using the Nuget Package Manager Console. Type the following command:

Install-Package DevTools.DataKitFramework


This package has dependencies on EntityFramework and Microsoft.AspNet.Identity.EntityFramework, both of which would be installed automatically.

Once the packages have been installed, add the connection string in the web.config file under just below the "configSections" element.


Next add a new class model under the "Models" folder:


 and replace the content of the class with the following codes:



Before we generate the configuration and migration class required by entity framework, let us build the project. Click on "Build" menu, "Build Solution" or "Ctrl+Shift+B" to build the project. After building project, enter the following commands on the Package Manger Console:

Enable-Migrations

followed by:

Add-Migration InitialDbState

This command will generate a migration class with two migration statements for creating two extra tables; "dbo.Settings" and "dbo.Tenants". This is normal but I am not focusing on the details for the extra tables, more on that in subsequent articles.

In the Global.asax.cs file, enter the following using statement:

using System.Data.Entity;
using AuditWebApp.Models;

And add the following statement in the "Application_Start" method:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppDbContext, Migrations.Configuration>());



Implementation

The first part of the process is now complete and we have a new web application project that is entityframework-enabled. In the next part, we are going to see how to use Dev Tools DataKit Framework to enable auditing. It is worth noting that internet connection is required for DataKit Framework to generate a working license in your first project run. If you are already working with this product and have made a purchase, then you can put your product key in the web.config file of your project under "appSettings" element.

<!-- Add your product key to the application settings of your web.config or app.config file. -->
<appSettings>
    <add key="DataKitFramework" value="{Your Product Key Here}" />
</appSettings>


If no purchase is done, no need to worry as DataKit Framework would run as a 14 days trial. But remember, you must have internet connection for your first project run.

Let's scaffold a controller with our model as this quickly gets us going by generating basic data-centric codes and accompanying views. In the "Solutions Explorer", right click on "Controllers", "Add", "Controller..." to display the "Add Scaffold" dialog.


In the dialog, choose "MVC 5 Controller with views, using Entity Framework" and click "Add".

In the "Add Controller" dialog, enter your model and database context in the provided fields. Check all appropriate fields and click on "Add" to scaffold the controller and views.



In this article I am focusing on auditing using DataKit Framework, but in my next articles, I will show you effective ways to do bulk insert, batch update and batch delete.

Let's go to the "Details.cshtml" view and make a few modifications as shown below:


Next add the modifications for the audit information as shown below:



In this article, I will only focus on the creation and modification dates but not on the user that performed the action since I've not done anything on user account and logins. But it is worth mentioning that DataKit Framework can monitor the logged on user and transfer the user identity to the records automatically. We would see that in subsequent articles.

Now if we run the project and enter the url as illustrated below, there would be a small delay and the products page would display. This is because DataKit Framework is trying to generate a license for you to work with. Remember you must have internet for this first run to generate the license. Subsequently, you don't need to have internet to run your codes.



Let's create a product and later take a look at it's details to see the auditing in effect.

Create Product page


Once the product is created it is displayed in the Product List page


We can view the Product Details page and see the "Created On" date, voilà!



In my next articles I would take a dive into bulk insert, batch updates/deletes, simple settings, dynamic filters and multi-tenancy. So coders get cracking and have a lovely day.




Share this post:

Facebook Google +

Get started in a Jiffy

Free articles on our software products

Get the most comprehensive tutorial on our products. Learn how to do the heavy-lifting with our written version articles. Video version is coming soon.

Get Started
Recent articles