|
1 | 1 | --- |
2 | | -Name: Audit |
3 | | -LastMod: 2025-06-24 |
| 2 | +Title: Auditing in Entity Framework Extensions |
| 3 | +MetaDescription: Learn how to use auditing options in Entity Framework Extensions for EF Core and EF6. Discover how to use `UseAudit`, `AuditEntries`, and `AuditEntry` to capture which data has been inserted, modified, or deleted in your database. |
| 4 | +LastMod: 2025-10-30 |
4 | 5 | --- |
5 | 6 |
|
6 | | -# Audit |
| 7 | +# 📜 Auditing in Entity Framework Extensions/n Capture data that has been inserted, modified, or deleted in your database |
7 | 8 |
|
8 | | -## Description |
| 9 | +The **audit feature** in Entity Framework Extensions lets you **track all changes** made to your database during any **bulk operation**. |
9 | 10 |
|
10 | | -The EFE audit feature allows you to track changes of all modifications that happened in the database during the Bulk Operations. Also, it lets you create a history of who modified a table, what the old value was and the new value was. |
| 11 | +Once captured, you can **log the old and new values** directly in your database, write them to a log file, or simply use the audit results within your application. |
| 12 | + |
| 13 | +The **audit feature** works with all bulk operations: |
| 14 | + |
| 15 | +* [BulkInsert](/bulk-insert) |
| 16 | +* [BulkInsertOptimized](/bulk-insert-optimized) |
| 17 | +* [BulkUpdate](/bulk-update) |
| 18 | +* [BulkDelete](/bulk-delete) |
| 19 | +* [BulkMerge](/bulk-merge) |
| 20 | +* [BulkSynchronize](/bulk-synchronize) |
| 21 | +* [BulkSaveChanges](/bulk-savechanges) |
11 | 22 |
|
12 | 23 | ## Key Features |
13 | 24 |
|
14 | | -- Allow getting a history of modifications |
15 | | -- Allow storing the history in a database or log file |
16 | | -- Allow tracking who, what, when a modification occurred |
17 | | -- Allow knowing what the old and the new value are |
| 25 | +* Keep a complete history of all modifications |
| 26 | +* [Store audit data in a database](/save-audit-history-in-a-database) or a log file |
| 27 | +* Track **who**, **what**, and **when** a modification occurred |
| 28 | +* Compare **old values** and **new values** for every change |
18 | 29 |
|
19 | 30 | ## Getting Started |
20 | 31 |
|
21 | 32 | To use the audit feature, you need to enable it and provide a list of the AuditEntry that will be populated during the operations. |
22 | 33 |
|
23 | 34 | ```csharp |
| 35 | +// @nuget: Z.EntityFramework.Extensions.EFCore |
| 36 | +using Z.BulkOperations; |
| 37 | +using Z.EntityFramework.Extensions; |
| 38 | + |
24 | 39 | // Execute |
25 | 40 | List<AuditEntry> auditEntries = new List<AuditEntry>(); |
26 | 41 | context.BulkMerge(list, options => |
27 | 42 | { |
28 | 43 | options.UseAudit = true; |
29 | 44 | options.AuditEntries = auditEntries; |
30 | 45 | }); |
| 46 | +``` |
| 47 | + |
| 48 | +## Class Definition |
| 49 | + |
| 50 | +The audit feature includes **three main classes**: |
| 51 | + |
| 52 | +* **AuditEntry**: Represents a row and contains information about the operation performed. |
| 53 | +* **AuditEntryItem**: Represents a column within a row and contains the old and new values. |
| 54 | +* **AuditActionType**: Defines the type of action performed (Insert, Update, Delete, etc.). |
| 55 | + |
| 56 | +```csharp |
| 57 | +public class AuditEntry |
| 58 | +{ |
| 59 | +public AuditActionType Action { get; set; } |
| 60 | +public DateTime Date { get; set; } |
| 61 | +public Dictionary<object, object> Metas { get; set; } // Hold any custom information you want to add |
| 62 | +public string TableName { get; set; } |
| 63 | +public List<AuditEntryItem> Values { get; set; } |
| 64 | +} |
| 65 | +``` |
31 | 66 |
|
| 67 | +```csharp |
| 68 | +public class AuditEntryItem |
| 69 | +{ |
| 70 | +public string ColumnName { get; set; } |
| 71 | +public object NewValue { get; set; } |
| 72 | +public object OldValue { get; set; } |
| 73 | +} |
32 | 74 | ``` |
33 | 75 |
|
34 | | -Try it: [.NET Core](https://dotnetfiddle.net/) | [.NET Framework](https://dotnetfiddle.net/) |
| 76 | +```csharp |
| 77 | +public enum AuditActionType |
| 78 | +{ |
| 79 | +Delete, |
| 80 | +Insert, |
| 81 | +Update, |
| 82 | +SoftDelete, |
| 83 | +} |
| 84 | +``` |
35 | 85 |
|
36 | | -## Scenarios |
| 86 | +## FAQ |
37 | 87 |
|
38 | | -- [Saving audit history in a database](../options/save-audit-history-in-a-database.md) |
39 | | -- Saving the audit history in a log file |
| 88 | +### Why does enabling this option decrease performance? |
40 | 89 |
|
41 | | -## Options |
| 90 | +When this option is enabled, **additional SQL statements** are required to return all old and new values. |
42 | 91 |
|
43 | | -| Name | Description | |
44 | | -|:-----------------------------------|:----------------------------------------------------------------------| |
45 | | -|[UseAudit](../options/use-audit.md) | Gets or sets the `UseAudit` property. When the `UseAudit` property is `true`, the [AuditEntries](../options/audit-entries.md) property stores auditing metadata about `INSERTED`, `UPDATED`, and `DELETED` rows and values. | |
46 | | -|[AuditEntries](../options/audit-entries.md) | Gets or sets the `AuditEntries` property. The `AuditEntries` property stores auditing metadata about `INSERTED`, `UPDATED`, and `DELETED` rows and values. This option requires setting the [UseAudit](../options/use-audit.md) property to `true`. | |
| 92 | +For some providers, such as **SQL Server**, it can no longer use `SqlBulkCopy` directly on the destination table when a column needs to be outputted. Instead, it must create a **temporary table**, which adds some extra overhead and reduces performance. |
47 | 93 |
|
48 | | -## Methods |
| 94 | +The impact can vary: |
49 | 95 |
|
50 | | -| Name | Description | |
51 | | -|:-----------------------------------|:----------------------------------------------------------------------| |
52 | | -|[AuditMode](../options/audit-mode.md) | The `AuditMode` method allows you to exclude or include properties from the auditing. | |
| 96 | +* It is **major** when the bulk strategy needs to be changed (for example, when `SqlBulkCopy` can’t be used). |
| 97 | +* It is **minor** when data was already being output, as the additional cost is limited. |
53 | 98 |
|
54 | | -## Entities |
| 99 | +## Conclusion |
55 | 100 |
|
56 | | -| Name | Description | |
57 | | -|:-----------------------------------|:----------------------------------------------------------------------| |
58 | | -|[AuditActionType](../options/audit-action-type.md) | The `AuditActionType` enum represents the action that has been performed (Delete, Insert or Update). | |
59 | | -|[AuditEntry](../options/audit-entry.md) | The `AuditEntry` class represents the auditing row metadata that has been modified. | |
60 | | -|[AuditEntryItem](../options/audit-entry-item.md) | The `AuditEntryItem` class represents the auditing value metadata of a row that has been modified. | |
61 | | -|[AuditModeType](../options/audit-mode-type.md) | The `AuditModeType` enum represents if all properties should be included or excluded from the auditing. The default value is `AuditModeType.IncludeAll`. | |
62 | | -|[ColumnMappingAuditModeType](../options/column-mapping-audit-mode-type.md) | The `ColumnMappingAuditModeType` enum represents if a specific property should be included or excluded from the auditing. The default value is `ColumnMappingAuditModeType.Inherit`. | |
| 101 | +The **audit feature** in Entity Framework Extensions is a powerful tool when you need to **keep track of what happens during your bulk operations**. |
63 | 102 |
|
64 | | -## FAQ |
| 103 | +With just a few lines of code, you can capture **who changed what and when**, store this information anywhere you want, and even compare old and new values for full transparency. |
65 | 104 |
|
66 | | -### Why enabling this option decreases the performance? |
| 105 | +While enabling auditing can slightly impact performance, it gives you complete visibility and control over your data changes — something that’s often worth the trade-off in real-world applications. |
67 | 106 |
|
68 | | -When enabling this option, additional SQL statements are required such as returning all old and new values. |
| 107 | +If you care about **data integrity, traceability, and accountability**, enabling auditing is one of the easiest ways to achieve it in your EF Core or EF6 projects. |
0 commit comments