如何彻底删除租户?
How to delete tenant completely?
我正在开发一个多租户应用程序,并且希望能够选择删除租户。然而,这似乎没有人们想象的那么微不足道。
我的目标是删除数据库中各处对租户的所有引用。我知道租户是软删除的,但我不想让我的数据库填满旧的无意义的数据,所以我尝试禁用软删除过滤器。
这是我试过的一些代码:
using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
{
await TenantRepository.DeleteAsync(x => x.Id == tenantId);
}
这没有用。租户标记为 "IsDeleted" 但未删除。
然后我认为它可能与 UnitOfWork 有关所以我确保没有 UnitOfWork 处于活动状态然后手动控制它:
using (var unitOfWork = _unitOfWorkManager.Begin())
{
// the codeblock above went here
unitOfWork.Complete();
}
这没有用,结果相同。这只是 AbpTenant table。我还试图从所有其他 table 中删除。例如 AbpSettings 和 AbpLanguages。我完全不清楚该怎么做——"managers" 不包含任何删除功能。
我尝试为这些实体创建 IRepository,但它不起作用。错误显示
The type Abo.Configuration.Setting cannot be used as a type parameter TEntity in the generic type or method IRepository. There is no implicit reference conversion from Abp.Configuration.Setting to Abo.Domain.Entities.IEntity.
这让我可以选择直接使用 DataContext:
using (EntityFramework.MyDbContext db = new EntityFramework.MyDbContext())
{
List<PermissionSetting> perms = await db.Permissions.Where(x => x.TenantId == tenantId).ToListAsync();
for (int i=0; i<perms.Count(); i++)
{
db.Permissions.Remove(perms[i]);
}
// I also tried deleting them in bulk at first
// ((DbSet<PermissionSetting>)db.Permissions).RemoveRange(db.Permissions.Where(x => x.TenantId == tenantId));
await db.SaveChangesAsync();
}
我尝试了使用和不使用 UnitOfWork。
但它并没有从数据库中删除。我没有收到任何错误或异常。
为什么没有被删除?我怎样才能删除它?一定可以吗?
since I don't want my database to fill up with old meaningless data I've tried disabling the soft-delete filter.
来自 上的问题:
protected override void CancelDeletionForSoftDelete(EntityEntry entry)
{
if (IsSoftDeleteFilterEnabled)
{
base.CancelDeletionForSoftDelete(entry);
}
}
The type Abo.Configuration.Setting cannot be used as a type parameter TEntity in the generic type or method IRepository. There is no implicit reference conversion from Abp.Configuration.Setting to Abo.Domain.Entities.IEntity.
注入 IRepository<Setting, long>
而不是 IRepository<Setting>
。
That leaves me with the option to use the DataContext directly
...
But it simply does not get deleted from the database. I'm getting no errors or Exceptions.
来自 Data Filters 上的文档:
using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant))
{
using (var db = new ...)
{
// ...
}
}
也就是说,没有办法轻易地完全删除相关的租户数据。考虑写 SQL.
我正在开发一个多租户应用程序,并且希望能够选择删除租户。然而,这似乎没有人们想象的那么微不足道。
我的目标是删除数据库中各处对租户的所有引用。我知道租户是软删除的,但我不想让我的数据库填满旧的无意义的数据,所以我尝试禁用软删除过滤器。
这是我试过的一些代码:
using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
{
await TenantRepository.DeleteAsync(x => x.Id == tenantId);
}
这没有用。租户标记为 "IsDeleted" 但未删除。 然后我认为它可能与 UnitOfWork 有关所以我确保没有 UnitOfWork 处于活动状态然后手动控制它:
using (var unitOfWork = _unitOfWorkManager.Begin())
{
// the codeblock above went here
unitOfWork.Complete();
}
这没有用,结果相同。这只是 AbpTenant table。我还试图从所有其他 table 中删除。例如 AbpSettings 和 AbpLanguages。我完全不清楚该怎么做——"managers" 不包含任何删除功能。
我尝试为这些实体创建 IRepository,但它不起作用。错误显示
The type Abo.Configuration.Setting cannot be used as a type parameter TEntity in the generic type or method IRepository. There is no implicit reference conversion from Abp.Configuration.Setting to Abo.Domain.Entities.IEntity.
这让我可以选择直接使用 DataContext:
using (EntityFramework.MyDbContext db = new EntityFramework.MyDbContext())
{
List<PermissionSetting> perms = await db.Permissions.Where(x => x.TenantId == tenantId).ToListAsync();
for (int i=0; i<perms.Count(); i++)
{
db.Permissions.Remove(perms[i]);
}
// I also tried deleting them in bulk at first
// ((DbSet<PermissionSetting>)db.Permissions).RemoveRange(db.Permissions.Where(x => x.TenantId == tenantId));
await db.SaveChangesAsync();
}
我尝试了使用和不使用 UnitOfWork。
但它并没有从数据库中删除。我没有收到任何错误或异常。
为什么没有被删除?我怎样才能删除它?一定可以吗?
since I don't want my database to fill up with old meaningless data I've tried disabling the soft-delete filter.
来自
protected override void CancelDeletionForSoftDelete(EntityEntry entry)
{
if (IsSoftDeleteFilterEnabled)
{
base.CancelDeletionForSoftDelete(entry);
}
}
The type Abo.Configuration.Setting cannot be used as a type parameter TEntity in the generic type or method IRepository. There is no implicit reference conversion from Abp.Configuration.Setting to Abo.Domain.Entities.IEntity.
注入 IRepository<Setting, long>
而不是 IRepository<Setting>
。
That leaves me with the option to use the DataContext directly ... But it simply does not get deleted from the database. I'm getting no errors or Exceptions.
来自 Data Filters 上的文档:
using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant))
{
using (var db = new ...)
{
// ...
}
}
也就是说,没有办法轻易地完全删除相关的租户数据。考虑写 SQL.