Support for third party modules, improved error handling, standardardized enum naming, reorganized interface definitions, support for DB script upgrades, added Settings entity
This commit is contained in:
59
Oqtane.Server/Repository/Context/MasterDBContext.cs
Normal file
59
Oqtane.Server/Repository/Context/MasterDBContext.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Models;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class MasterDBContext : DbContext
|
||||
{
|
||||
private IHttpContextAccessor accessor;
|
||||
|
||||
public MasterDBContext(DbContextOptions<MasterDBContext> options, IHttpContextAccessor accessor) : base(options)
|
||||
{
|
||||
this.accessor = accessor;
|
||||
}
|
||||
|
||||
public virtual DbSet<Alias> Alias { get; set; }
|
||||
public virtual DbSet<Tenant> Tenant { get; set; }
|
||||
|
||||
public override int SaveChanges()
|
||||
{
|
||||
ChangeTracker.DetectChanges();
|
||||
|
||||
string username = "";
|
||||
if (accessor.HttpContext.User.Identity.Name != null)
|
||||
{
|
||||
username = accessor.HttpContext.User.Identity.Name;
|
||||
}
|
||||
DateTime date = DateTime.Now;
|
||||
|
||||
var created = ChangeTracker.Entries()
|
||||
.Where(x => x.State == EntityState.Added);
|
||||
|
||||
foreach (var item in created)
|
||||
{
|
||||
if (item.Entity is IAuditable entity)
|
||||
{
|
||||
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 entity)
|
||||
{
|
||||
item.CurrentValues[nameof(IAuditable.ModifiedBy)] = username;
|
||||
item.CurrentValues[nameof(IAuditable.ModifiedOn)] = date;
|
||||
}
|
||||
}
|
||||
|
||||
return base.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user