/
EntityFrameworkEvent.cs
81 lines (76 loc) · 2.82 KB
/
EntityFrameworkEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Collections.Generic;
using Audit.Core;
#if EF_CORE
using Microsoft.EntityFrameworkCore;
#else
using System.Data.Entity;
#endif
using System.Text.Json.Serialization;
namespace Audit.EntityFramework
{
public class EntityFrameworkEvent : IAuditOutput
{
/// <summary>
/// The database name
/// </summary>
public string Database { get; set; }
/// <summary>
/// A unique identifier for the database connection.
/// This identifier is primarily intended as a correlation ID for logging and debugging such
/// that it is easy to identify that multiple events are using the same or different database connection.
/// </summary>
public string ConnectionId { get; set; }
#if EF_CORE_5_OR_GREATER
/// <summary>
/// A unique identifier for the context instance and pool lease, if any.
/// This identifier is primarily intended as a correlation ID for logging and debugging such
/// that it is easy to identify that multiple events are using the same or different context instances.
/// </summary>
public string ContextId { get; set; }
#endif
/// <summary>
/// The ambient transaction identifier, if any.
/// </summary>
public string AmbientTransactionId { get; set; }
/// <summary>
/// The local transaction identifier, if any.
/// </summary>
public string TransactionId { get; set; }
/// <summary>
/// Collection of affected entities
/// </summary>
public List<EventEntry> Entries { get; set; }
#if EF_FULL
public List<AssociationEntry> Associations { get; set; }
#endif
public int Result { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
[JsonIgnore]
internal DbContext DbContext { get; set; }
[JsonExtensionData]
public Dictionary<string, object> CustomFields { get; set; } = new Dictionary<string, object>();
/// <summary>
/// Returns the DbContext associated to this event
/// </summary>
public DbContext GetDbContext()
{
return DbContext;
}
/// <summary>
/// Serializes this Entity Framework event as a JSON string
/// </summary>
public string ToJson()
{
return Core.Configuration.JsonAdapter.Serialize(this);
}
/// <summary>
/// Parses an Entity Framework event from its JSON string representation.
/// </summary>
/// <param name="json">JSON string with the Entity Framework event representation.</param>
public static EntityFrameworkEvent FromJson(string json)
{
return Core.Configuration.JsonAdapter.Deserialize<EntityFrameworkEvent>(json);
}
}
}