Updated the Installation of Oqtane to use Migrations
This commit is contained in:
@ -7,48 +7,56 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Oqtane.Extensions;
|
||||
using Oqtane.Models;
|
||||
// ReSharper disable BuiltInTypeReferenceStyleForMemberAccess
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class DBContextBase : IdentityUserContext<IdentityUser>
|
||||
{
|
||||
private ITenantResolver _tenantResolver;
|
||||
private IHttpContextAccessor _accessor;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IDbConfig _dbConfig;
|
||||
private readonly ITenantResolver _tenantResolver;
|
||||
|
||||
public DBContextBase(ITenantResolver tenantResolver, IHttpContextAccessor accessor, IConfiguration configuration)
|
||||
public DBContextBase(IDbConfig dbConfig, ITenantResolver tenantResolver)
|
||||
{
|
||||
_dbConfig = dbConfig;
|
||||
_tenantResolver = tenantResolver;
|
||||
_accessor = accessor;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var tenant = _tenantResolver.GetTenant();
|
||||
if (tenant != null)
|
||||
var connectionString = _dbConfig.ConnectionString;
|
||||
|
||||
if (string.IsNullOrEmpty(connectionString) && _tenantResolver != null)
|
||||
{
|
||||
var tenant = _tenantResolver.GetTenant();
|
||||
var configuration = _dbConfig.Configuration;
|
||||
|
||||
if (tenant != null)
|
||||
{
|
||||
connectionString = tenant.DBConnectionString
|
||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!String.IsNullOrEmpty(configuration.GetConnectionString("DefaultConnection")))
|
||||
{
|
||||
connectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
var connectionString = tenant.DBConnectionString
|
||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||
optionsBuilder.UseOqtaneDatabase(connectionString);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!String.IsNullOrEmpty(_configuration.GetConnectionString("DefaultConnection")))
|
||||
{
|
||||
var connectionString = _configuration.GetConnectionString("DefaultConnection")
|
||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||
|
||||
optionsBuilder.UseOqtaneDatabase(connectionString);
|
||||
}
|
||||
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public override int SaveChanges()
|
||||
{
|
||||
DbContextUtils.SaveChanges(this, _accessor);
|
||||
DbContextUtils.SaveChanges(this, _dbConfig.Accessor);
|
||||
|
||||
return base.SaveChanges();
|
||||
}
|
||||
|
20
Oqtane.Server/Repository/Context/DbConfig.cs
Normal file
20
Oqtane.Server/Repository/Context/DbConfig.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class DbConfig : IDbConfig
|
||||
{
|
||||
public DbConfig(IHttpContextAccessor accessor, IConfiguration configuration)
|
||||
{
|
||||
Accessor = accessor;
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IHttpContextAccessor Accessor { get; }
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class DbContextUtils
|
||||
public static class DbContextUtils
|
||||
{
|
||||
public static void SaveChanges(DbContext context, IHttpContextAccessor accessor)
|
||||
{
|
||||
|
@ -22,5 +22,8 @@ namespace Oqtane.Repository
|
||||
public virtual DbSet<Tenant> Tenant { get; set; }
|
||||
public virtual DbSet<ModuleDefinition> ModuleDefinition { get; set; }
|
||||
public virtual DbSet<Job> Job { get; set; }
|
||||
public virtual DbSet<JobLog> JobLog { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +1,42 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Oqtane.Extensions;
|
||||
|
||||
// ReSharper disable BuiltInTypeReferenceStyleForMemberAccess
|
||||
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
||||
// ReSharper disable CheckNamespace
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class MasterDBContext : DbContext
|
||||
{
|
||||
private readonly IHttpContextAccessor _accessor;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IDbConfig _dbConfig;
|
||||
|
||||
public MasterDBContext(DbContextOptions<MasterDBContext> options, IHttpContextAccessor accessor, IConfiguration configuration) : base(options)
|
||||
public MasterDBContext(DbContextOptions<MasterDBContext> options, IDbConfig dbConfig) : base(options)
|
||||
{
|
||||
_accessor = accessor;
|
||||
_configuration = configuration;
|
||||
_dbConfig = dbConfig;
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(_configuration.GetConnectionString("DefaultConnection")))
|
||||
{
|
||||
var connectionString = _configuration.GetConnectionString("DefaultConnection")
|
||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||
var connectionString = _dbConfig.ConnectionString;
|
||||
var configuration = _dbConfig.Configuration;
|
||||
|
||||
if(string.IsNullOrEmpty(connectionString) && configuration != null)
|
||||
{
|
||||
if (!String.IsNullOrEmpty(configuration.GetConnectionString("DefaultConnection")))
|
||||
{
|
||||
connectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(connectionString))
|
||||
{
|
||||
optionsBuilder.UseOqtaneDatabase(connectionString);
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
@ -39,7 +50,7 @@ namespace Oqtane.Repository
|
||||
|
||||
public override int SaveChanges()
|
||||
{
|
||||
DbContextUtils.SaveChanges(this, _accessor);
|
||||
DbContextUtils.SaveChanges(this, _dbConfig.Accessor);
|
||||
|
||||
return base.SaveChanges();
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ namespace Oqtane.Repository
|
||||
|
||||
public virtual DbSet<Language> Language { get; set; }
|
||||
|
||||
public TenantDBContext(ITenantResolver tenantResolver, IHttpContextAccessor accessor, IConfiguration configuration) : base(tenantResolver, accessor, configuration)
|
||||
public TenantDBContext(IDbConfig dbConfig, ITenantResolver tenantResolver) : base(dbConfig, tenantResolver)
|
||||
{
|
||||
// DBContextBase handles multi-tenant database connections
|
||||
}
|
||||
|
14
Oqtane.Server/Repository/Interfaces/IDbConfig.cs
Normal file
14
Oqtane.Server/Repository/Interfaces/IDbConfig.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public interface IDbConfig
|
||||
{
|
||||
public IHttpContextAccessor Accessor { get; }
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
using System.Data.SqlClient;
|
||||
using System.Reflection;
|
||||
using System.Reflection;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
|
@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
@ -59,8 +59,8 @@ namespace Oqtane.Repository
|
||||
|
||||
public int ExecuteNonQuery(Tenant tenant, string query)
|
||||
{
|
||||
SqlConnection conn = new SqlConnection(FormatConnectionString(tenant.DBConnectionString));
|
||||
SqlCommand cmd = conn.CreateCommand();
|
||||
var conn = new SqlConnection(FormatConnectionString(tenant.DBConnectionString));
|
||||
var cmd = conn.CreateCommand();
|
||||
using (conn)
|
||||
{
|
||||
PrepareCommand(conn, cmd, query);
|
||||
|
Reference in New Issue
Block a user