fix context delete models: dbtenant, dbtariff from BackupsContext

This commit is contained in:
diana-vahomskaya 2021-08-12 18:18:01 +03:00
parent 63cceb5ca6
commit 5e22019a28
3 changed files with 23 additions and 15 deletions

View File

@ -12,6 +12,8 @@ using System.Xml.XPath;
using ASC.Common;
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Data.Backup.EF.Context;
using Microsoft.EntityFrameworkCore;
@ -30,11 +32,15 @@ namespace ASC.Data.Backup
private readonly IDictionary<string, string> whereExceptions = new Dictionary<string, string>();
private readonly ILog log;
private readonly BackupsContext backupsContext;
private readonly TenantDbContext tenantDbContext;
private readonly CoreDbContext coreDbContext;
public DbHelper(IOptionsMonitor<ILog> options, ConnectionStringSettings connectionString, BackupsContext backupsContext)
public DbHelper(IOptionsMonitor<ILog> options, ConnectionStringSettings connectionString, BackupsContext backupsContext, TenantDbContext tenantDbContext, CoreDbContext coreDbContext)
{
log = options.CurrentValue;
this.backupsContext = backupsContext;
this.tenantDbContext = tenantDbContext;
this.coreDbContext = coreDbContext;
var file = connectionString.ElementInformation.Source;
if ("web.connections.config".Equals(Path.GetFileName(file), StringComparison.InvariantCultureIgnoreCase))
{
@ -150,11 +156,11 @@ namespace ASC.Data.Backup
if ("tenants_tenants".Equals(table.TableName, StringComparison.InvariantCultureIgnoreCase))
{
// remove last tenant
var tenant = backupsContext.Tenants.LastOrDefault();
var tenant = tenantDbContext.Tenants.LastOrDefault();
if (tenant != null)
{
backupsContext.Tenants.Remove(tenant);
backupsContext.SaveChanges();
tenantDbContext.Tenants.Remove(tenant);
tenantDbContext.SaveChanges();
}
/* var tenantid = CreateCommand("select id from tenants_tenants order by id desc limit 1").ExecuteScalar();
CreateCommand("delete from tenants_tenants where id = " + tenantid).ExecuteNonQuery();*/
@ -165,11 +171,11 @@ namespace ASC.Data.Backup
r[table.Columns["mappeddomain"]] = null;
if (table.Columns.Contains("id"))
{
var tariff = backupsContext.Tariffs.FirstOrDefault(t => t.Tenant == tenant.Id);
var tariff = coreDbContext.Tariffs.FirstOrDefault(t => t.Tenant == tenant.Id);
tariff.Tenant = (int)r[table.Columns["id"]];
// CreateCommand("update tenants_tariff set tenant = " + r[table.Columns["id"]] + " where tenant = " + tenantid).ExecuteNonQuery();
backupsContext.Entry(tariff).State = EntityState.Modified;
backupsContext.SaveChanges();
coreDbContext.Entry(tariff).State = EntityState.Modified;
coreDbContext.SaveChanges();
}
}
}

View File

@ -12,8 +12,6 @@ namespace ASC.Data.Backup.EF.Context
{
public DbSet<BackupRecord> Backups { get; set; }
public DbSet<BackupSchedule> Schedules { get; set; }
public DbSet<DbTenant> Tenants { get; set; }
public DbSet<DbTariff> Tariffs { get; set; }
public BackupsContext() { }
public BackupsContext(DbContextOptions<BackupsContext> options)

View File

@ -30,6 +30,7 @@ using System.Linq;
using ASC.Common;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Tenants;
using ASC.Data.Backup.EF.Context;
using ASC.Data.Backup.EF.Model;
@ -40,10 +41,13 @@ namespace ASC.Data.Backup.Storage
{
private Lazy<BackupsContext> LazyBackupsContext { get; }
private BackupsContext BackupContext { get => LazyBackupsContext.Value; }
private Lazy<TenantDbContext> LazyTenantDbContext { get; }
private TenantDbContext TenantDbContext { get => LazyTenantDbContext.Value; }
public BackupRepository(DbContextManager<BackupsContext> backupContext)
public BackupRepository(DbContextManager<BackupsContext> backupContext, DbContextManager<TenantDbContext> tenantDbContext)
{
LazyBackupsContext = new Lazy<BackupsContext>(() => backupContext.Value);
LazyTenantDbContext = new Lazy<TenantDbContext>(() => tenantDbContext.Value);
}
public void SaveBackupRecord(BackupRecord backup)
@ -103,7 +107,7 @@ namespace ASC.Data.Backup.Storage
public List<BackupSchedule> GetBackupSchedules()
{
var query = BackupContext.Schedules.Join(BackupContext.Tenants,
var query = BackupContext.Schedules.Join(TenantDbContext.Tenants,
s => s.TenantId,
t => t.Id,
(s, t) => new { schedule = s, tenant = t })