commit
78aabaafb3
|
@ -36,7 +36,7 @@ else
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<label>@Localizer["Rows:"] </label>
|
<label>@Localizer["Maximum Records:"] </label>
|
||||||
<select class="form-control" @onchange="(e => RowsChanged(e))">
|
<select class="form-control" @onchange="(e => RowsChanged(e))">
|
||||||
<option value="10">10</option>
|
<option value="10">10</option>
|
||||||
<option value="50">50</option>
|
<option value="50">50</option>
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Oqtane.Extensions
|
||||||
|
{
|
||||||
|
public static class DbContextOptionsBuilderExtensions
|
||||||
|
{
|
||||||
|
public static DbContextOptionsBuilder UseOqtaneDatabase([NotNull] this DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseSqlServer(connectionString);
|
||||||
|
|
||||||
|
return optionsBuilder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -168,7 +168,8 @@ namespace Oqtane.Infrastructure
|
||||||
var dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString();
|
var dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString();
|
||||||
if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory);
|
if (!Directory.Exists(dataDirectory)) Directory.CreateDirectory(dataDirectory);
|
||||||
|
|
||||||
using (var dbc = new DbContext(new DbContextOptionsBuilder().UseSqlServer(NormalizeConnectionString(install.ConnectionString)).Options))
|
var connectionString = NormalizeConnectionString(install.ConnectionString);
|
||||||
|
using (var dbc = new DbContext(new DbContextOptionsBuilder().UseOqtaneDatabase(connectionString).Options))
|
||||||
{
|
{
|
||||||
// create empty database if it does not exist
|
// create empty database if it does not exist
|
||||||
dbc.Database.EnsureCreated();
|
dbc.Database.EnsureCreated();
|
||||||
|
|
|
@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Oqtane.Extensions;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
|
|
||||||
namespace Oqtane.Repository
|
namespace Oqtane.Repository
|
||||||
|
@ -24,63 +25,16 @@ namespace Oqtane.Repository
|
||||||
var tenant = _tenantResolver.GetTenant();
|
var tenant = _tenantResolver.GetTenant();
|
||||||
if (tenant != null)
|
if (tenant != null)
|
||||||
{
|
{
|
||||||
optionsBuilder.UseSqlServer(tenant.DBConnectionString
|
var connectionString = tenant.DBConnectionString
|
||||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString())
|
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||||
);
|
optionsBuilder.UseOqtaneDatabase(connectionString);
|
||||||
}
|
}
|
||||||
base.OnConfiguring(optionsBuilder);
|
base.OnConfiguring(optionsBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int SaveChanges()
|
public override int SaveChanges()
|
||||||
{
|
{
|
||||||
ChangeTracker.DetectChanges();
|
DbContextUtils.SaveChanges(this, _accessor);
|
||||||
|
|
||||||
string username = "";
|
|
||||||
if (_accessor.HttpContext != null && _accessor.HttpContext.User.Identity.Name != null)
|
|
||||||
{
|
|
||||||
username = _accessor.HttpContext.User.Identity.Name;
|
|
||||||
}
|
|
||||||
DateTime date = DateTime.UtcNow;
|
|
||||||
|
|
||||||
var created = ChangeTracker.Entries()
|
|
||||||
.Where(x => x.State == EntityState.Added);
|
|
||||||
|
|
||||||
foreach(var item in created)
|
|
||||||
{
|
|
||||||
if (item.Entity is IAuditable)
|
|
||||||
{
|
|
||||||
item.CurrentValues[nameof(IAuditable.CreatedBy)] = username;
|
|
||||||
item.CurrentValues[nameof(IAuditable.CreatedOn)] = date;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var modified = ChangeTracker.Entries()
|
|
||||||
.Where(x => x.State == EntityState.Modified || x.State == EntityState.Added);
|
|
||||||
|
|
||||||
foreach (var item in modified)
|
|
||||||
{
|
|
||||||
if (item.Entity is IAuditable)
|
|
||||||
{
|
|
||||||
item.CurrentValues[nameof(IAuditable.ModifiedBy)] = username;
|
|
||||||
item.CurrentValues[nameof(IAuditable.ModifiedOn)] = date;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.Entity is IDeletable && item.State != EntityState.Added)
|
|
||||||
{
|
|
||||||
if ((bool)item.CurrentValues[nameof(IDeletable.IsDeleted)]
|
|
||||||
&& !item.GetDatabaseValues().GetValue<bool>(nameof(IDeletable.IsDeleted)))
|
|
||||||
{
|
|
||||||
item.CurrentValues[nameof(IDeletable.DeletedBy)] = username;
|
|
||||||
item.CurrentValues[nameof(IDeletable.DeletedOn)] = date;
|
|
||||||
}
|
|
||||||
else if (!(bool)item.CurrentValues[nameof(IDeletable.IsDeleted)]
|
|
||||||
&& item.GetDatabaseValues().GetValue<bool>(nameof(IDeletable.IsDeleted)))
|
|
||||||
{
|
|
||||||
item.CurrentValues[nameof(IDeletable.DeletedBy)] = null;
|
|
||||||
item.CurrentValues[nameof(IDeletable.DeletedOn)] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return base.SaveChanges();
|
return base.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
65
Oqtane.Server/Repository/Context/DbContextUtils.cs
Normal file
65
Oqtane.Server/Repository/Context/DbContextUtils.cs
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Oqtane.Models;
|
||||||
|
|
||||||
|
namespace Oqtane.Repository
|
||||||
|
{
|
||||||
|
public class DbContextUtils
|
||||||
|
{
|
||||||
|
public static void SaveChanges(DbContext context, IHttpContextAccessor accessor)
|
||||||
|
{
|
||||||
|
var changeTracker = context.ChangeTracker;
|
||||||
|
|
||||||
|
changeTracker.DetectChanges();
|
||||||
|
|
||||||
|
string username = "";
|
||||||
|
if (accessor.HttpContext != null && accessor.HttpContext.User.Identity.Name != null)
|
||||||
|
{
|
||||||
|
username = accessor.HttpContext.User.Identity.Name;
|
||||||
|
}
|
||||||
|
DateTime date = DateTime.UtcNow;
|
||||||
|
|
||||||
|
var created = changeTracker.Entries()
|
||||||
|
.Where(x => x.State == EntityState.Added);
|
||||||
|
|
||||||
|
foreach(var item in created)
|
||||||
|
{
|
||||||
|
if (item.Entity is IAuditable)
|
||||||
|
{
|
||||||
|
item.CurrentValues[nameof(IAuditable.CreatedBy)] = username;
|
||||||
|
item.CurrentValues[nameof(IAuditable.CreatedOn)] = date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var modified = changeTracker.Entries()
|
||||||
|
.Where(x => x.State == EntityState.Modified || x.State == EntityState.Added);
|
||||||
|
|
||||||
|
foreach (var item in modified)
|
||||||
|
{
|
||||||
|
if (item.Entity is IAuditable)
|
||||||
|
{
|
||||||
|
item.CurrentValues[nameof(IAuditable.ModifiedBy)] = username;
|
||||||
|
item.CurrentValues[nameof(IAuditable.ModifiedOn)] = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.Entity is IDeletable && item.State != EntityState.Added)
|
||||||
|
{
|
||||||
|
if ((bool)item.CurrentValues[nameof(IDeletable.IsDeleted)]
|
||||||
|
&& !item.GetDatabaseValues().GetValue<bool>(nameof(IDeletable.IsDeleted)))
|
||||||
|
{
|
||||||
|
item.CurrentValues[nameof(IDeletable.DeletedBy)] = username;
|
||||||
|
item.CurrentValues[nameof(IDeletable.DeletedOn)] = date;
|
||||||
|
}
|
||||||
|
else if (!(bool)item.CurrentValues[nameof(IDeletable.IsDeleted)]
|
||||||
|
&& item.GetDatabaseValues().GetValue<bool>(nameof(IDeletable.IsDeleted)))
|
||||||
|
{
|
||||||
|
item.CurrentValues[nameof(IDeletable.DeletedBy)] = null;
|
||||||
|
item.CurrentValues[nameof(IDeletable.DeletedOn)] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Oqtane.Extensions;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
|
|
||||||
namespace Oqtane.Repository
|
namespace Oqtane.Repository
|
||||||
|
@ -15,7 +16,7 @@ namespace Oqtane.Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
=> optionsBuilder.UseSqlServer(_connectionString);
|
=> optionsBuilder.UseOqtaneDatabase(_connectionString);
|
||||||
|
|
||||||
public virtual DbSet<Alias> Alias { get; set; }
|
public virtual DbSet<Alias> Alias { get; set; }
|
||||||
public virtual DbSet<Tenant> Tenant { get; set; }
|
public virtual DbSet<Tenant> Tenant { get; set; }
|
||||||
|
|
|
@ -4,13 +4,14 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Oqtane.Extensions;
|
||||||
|
|
||||||
namespace Oqtane.Repository
|
namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
public class MasterDBContext : DbContext
|
public class MasterDBContext : DbContext
|
||||||
{
|
{
|
||||||
private IHttpContextAccessor _accessor;
|
private readonly IHttpContextAccessor _accessor;
|
||||||
private IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
public MasterDBContext(DbContextOptions<MasterDBContext> options, IHttpContextAccessor accessor, IConfiguration configuration) : base(options)
|
public MasterDBContext(DbContextOptions<MasterDBContext> options, IHttpContextAccessor accessor, IConfiguration configuration) : base(options)
|
||||||
{
|
{
|
||||||
|
@ -20,11 +21,12 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(_configuration.GetConnectionString("DefaultConnection")))
|
if (!String.IsNullOrEmpty(_configuration.GetConnectionString("DefaultConnection")))
|
||||||
{
|
{
|
||||||
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")
|
var connectionString = _configuration.GetConnectionString("DefaultConnection")
|
||||||
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString())
|
.Replace("|DataDirectory|", AppDomain.CurrentDomain.GetData("DataDirectory")?.ToString());
|
||||||
);
|
|
||||||
|
optionsBuilder.UseOqtaneDatabase(connectionString);
|
||||||
}
|
}
|
||||||
base.OnConfiguring(optionsBuilder);
|
base.OnConfiguring(optionsBuilder);
|
||||||
}
|
}
|
||||||
|
@ -37,38 +39,7 @@ namespace Oqtane.Repository
|
||||||
|
|
||||||
public override int SaveChanges()
|
public override int SaveChanges()
|
||||||
{
|
{
|
||||||
ChangeTracker.DetectChanges();
|
DbContextUtils.SaveChanges(this, _accessor);
|
||||||
|
|
||||||
string username = "";
|
|
||||||
if (_accessor.HttpContext != null && _accessor.HttpContext.User.Identity.Name != null)
|
|
||||||
{
|
|
||||||
username = _accessor.HttpContext.User.Identity.Name;
|
|
||||||
}
|
|
||||||
DateTime date = DateTime.UtcNow;
|
|
||||||
|
|
||||||
var created = ChangeTracker.Entries()
|
|
||||||
.Where(x => x.State == EntityState.Added);
|
|
||||||
|
|
||||||
foreach (var item in created)
|
|
||||||
{
|
|
||||||
if (item.Entity is IAuditable)
|
|
||||||
{
|
|
||||||
item.CurrentValues[nameof(IAuditable.CreatedBy)] = username;
|
|
||||||
item.CurrentValues[nameof(IAuditable.CreatedOn)] = date;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var modified = ChangeTracker.Entries()
|
|
||||||
.Where(x => x.State == EntityState.Modified || x.State == EntityState.Added);
|
|
||||||
|
|
||||||
foreach (var item in modified)
|
|
||||||
{
|
|
||||||
if (item.Entity is IAuditable)
|
|
||||||
{
|
|
||||||
item.CurrentValues[nameof(IAuditable.ModifiedBy)] = username;
|
|
||||||
item.CurrentValues[nameof(IAuditable.ModifiedOn)] = date;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return base.SaveChanges();
|
return base.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user