Repository navigation
Expand file tree
/
Copy pathModuleDbContext.cs
More file actions
101 lines (90 loc) · 3.92 KB
/
Copy pathModuleDbContext.cs
File metadata and controls
101 lines (90 loc) · 3.92 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// MIT License
// Copyright DNN Community
namespace DNN.Modules.UserVoice.Data
{
using DNN.Modules.UserVoice.Data.Entities;
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Web;
/// <summary>
/// The data context for this module.
/// </summary>
[ExcludeFromCodeCoverage] // Very close to the metal configuration code, hard to test without heavy mocking, and low risk of bugs.
public class ModuleDbContext : DbContext
{
/// <summary>
/// Cached connection string, computed lazily and thread-safe.
/// </summary>
private static readonly Lazy<string> CachedConnectionString = new Lazy<string>(ComputeConnectionString);
/// <summary>
/// Initializes a new instance of the <see cref="ModuleDbContext"/> class.
/// </summary>
public ModuleDbContext()
: base(GetConnectionString())
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ModuleDbContext, Migrations.Configuration>());
}
/// <summary>
/// Initializes a new instance of the <see cref="ModuleDbContext"/> class.
/// </summary>
/// <param name="connection">An existing <see cref="DbConnection"/>.</param>
public ModuleDbContext(DbConnection connection)
: base(connection, true)
{
Database.SetInitializer(new NullDatabaseInitializer<ModuleDbContext>());
this.Database.CreateIfNotExists();
}
/// <summary>
/// Gets or sets the ideas.
/// </summary>
public virtual DbSet<Idea> Ideas { get; set; }
/// <summary>
/// Gets or sets the collection of user vote entities in the database.
/// </summary>
public virtual DbSet<UserVote> UserVotes { get; set; }
/// <summary>
/// Gets the connection string, using cached value after first call.
/// </summary>
/// <returns>The connection string.</returns>
private static string GetConnectionString()
{
return CachedConnectionString.Value;
}
/// <summary>
/// Computes the connection string by checking runtime context and configuration files.
/// This method is called only once per application lifetime.
/// </summary>
/// <returns>The connection string.</returns>
private static string ComputeConnectionString()
{
// For runtime
if (HttpContext.Current != null)
{
return "name=SiteSqlServer";
}
// For CLI tools, start from the current base directory and move up until we find web.config
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string configFilePath = Path.Combine(baseDirectory, "web.config");
while (!File.Exists(configFilePath) && baseDirectory != null)
{
baseDirectory = Directory.GetParent(baseDirectory)?.FullName;
if (baseDirectory == null)
{
throw new FileNotFoundException("web.config not found in any parent directory.");
}
configFilePath = Path.Combine(baseDirectory, "web.config");
}
// Load the configuration manually from the web.config file
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var connectionString = config.ConnectionStrings.ConnectionStrings["SiteSqlServer"];
return connectionString == null
? throw new NullReferenceException("Connection string 'SiteSqlServer' not found.")
: connectionString.ConnectionString;
}
}
}