Entity Framework saves entities in a database but doesn’t let you easily track changes, by example, a history of all modifications and their author in an audit table.
EF+ Audit easily tracks changes, exclude/include entity or property and auto save audit entries in the database.
You need to automatically save audit entries in the database to keep a history in an audit table.
If an action for the property AutoSavePreAction is set, audit entries will automatically be saved in the database when SaveChanges or SaveChangesAsync methods are called.
By using EF+ Audit entity
By using a different context
Custom AuditEntry & Database First Approach
Saving automatically by overriding SaveChanges & SaveChangesAsync
SQL Script (for Database First)
You want to use DataAnnotations.
Following DataAnnotations is available:
However, in order to make them work, you must enable DataAnnotations
Example Enable DataAnnotations
Exclude & Include Entity
You need to audit only a subset of your entities and you need to exclude/include by entity types, base class or interface.
Exclude/Include entity methods:
Exclude & Include Property
You need to audit only a subset of your properties and you need to exclude/include properties by name and by entity type, base class or interface.
Exclude/Include property methods:
You need to format a value with a different string representation by example adding a dollar sign to the money value.
Format value method:
Format< T >(selector, formatter)
You need to ignore a relationship or a subset of all available events.
Ignore event properties:
You need to keep track of all changed and unchanged properties
You can choose to ignore or not property unchanged with IgnorePropertyUnchanged.
By default, properties unchanged are ignored unless it’s part of the primary key.
Soft Add & Soft Delete
You need to keep an audit when a soft add/restore or a soft delete event happens.
Soft Add & Soft Delete methods:
When an entity satisfies the predicate, the audit entry state will be changed from “EntityModified” to either “EntitySoftAdded” or “EntitySoftDeleted”.
Retrieve AuditEntries for specific item
You want to retrieve all AuditEntry for a specific item
You can filter the AuditEntries DbSet using Where method and providing either the item or the key.
Audit, AuditEntry & AuditEntryProperty
AuditEntry and AuditEntryProperty can be added in your context to automatically save audit entries using the AutoSavePreAction property. Only mapped properties are mapped to your database.
Properties (Not Mapped)
NewValueFormatted (Mapped to NewValue)
OldValueFormatted (Mapped to OldValue)
Properties (Not Mapped)
You want to customize some fields value like CreatedDate or even more, want to use your own class inheriting from AuditEntry && AuditEntryProperty?
Use AuditEntryFactory && AuditEntryPropertyFactory
Custom DbSet with AutoSave
Audit + Entity Framework Extensions
If you are using Entity Framework Extensions, is still possible to use the EF+ Audit features.
However, it will only work with BulkSaveChanges.
Other Bulk Operations (BulkInsert, BulkUpdate, BulkDelete, and BulkMerge) doesn’t use the Change Tracker so, there is nothing to track!
BulkSaveChanges + AutoSave
Entity Framework 7:
DO NOT support relationship
DO NOT populate EntitySetName value
EF+ Audit: Full version or Standalone version
Database Provider: All supported
Entity Framework Version: EF5, EF6, EF7
Minimum Framework Version: .NET Framework 4
Why only my key is added when updating my entity?
This issue often happen for MVC user. They create a new entity through HttpPost values and force the state to “Modified”, the context is not aware of the original value and use the current value instead. So every property has the original value == current value and our auditing only log the key since all other values are equals.
We recommend to set the **IgnorePropertyUnchanged **to false to log every property.
Here is an example of this issue: Issues #8
Auditing in Entity Framework could not be simpler, there are always some entities in an application where an audit table can be crucial to keep track of what’s happening and you now have access to an easy to use library for these situations.