-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathDefaultAuditDbContext.cs
88 lines (77 loc) · 2.78 KB
/
DefaultAuditDbContext.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
82
83
84
85
86
87
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
namespace Audit.SqlServer
{
#if !NET7_0_OR_GREATER
/// <summary>
/// Represents the model to use to query the AuditEvent Id and Value with Entity Framework
/// </summary>
public class AuditEventValueModel
{
/// <summary>
/// The AuditEvent Value
/// </summary>
[System.ComponentModel.DataAnnotations.Key]
public string Value { get; set; }
}
#endif
/// <summary>
/// Represents the default DbContext for Audit.NET.SqlServer to store the events
/// </summary>
public class DefaultAuditDbContext : DbContext
{
private string _connectionString = null;
private DbConnection _dbConnection = null;
/// <summary>
/// Creates a new instance of DefaultAuditDbContext using the provided connection string
/// </summary>
/// <param name="connectionString">The connection string</param>
/// <param name="options">The DbContext options</param>
public DefaultAuditDbContext(string connectionString, DbContextOptions options) : base(options)
{
_connectionString = connectionString;
}
/// <summary>
/// Creates a new instance of DefaultAuditDbContext using the provided DB connection
/// </summary>
/// <param name="dbConnection"></param>
/// <param name="options"></param>
public DefaultAuditDbContext(DbConnection dbConnection, DbContextOptions options) : base(options)
{
_dbConnection = dbConnection;
}
/// <summary>
/// Creates a new instance of DefaultAuditDbContext using the provided connection string
/// </summary>
/// <param name="connectionString">The connection string</param>
public DefaultAuditDbContext(string connectionString)
{
_connectionString = connectionString;
}
/// <summary>
/// Creates a new instance of DefaultAuditDbContext using the provided DB connection
/// </summary>
/// <param name="connection">The DB connection</param>
public DefaultAuditDbContext(DbConnection connection)
{
_dbConnection = connection;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (_dbConnection != null)
{
optionsBuilder.UseSqlServer(_dbConnection);
}
else if (_connectionString != null)
{
optionsBuilder.UseSqlServer(_connectionString);
}
}
#if !NET7_0_OR_GREATER
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditEventValueModel>();
}
#endif
}
}