asp.net-mvc - 如何彻底删除租户?

标签 asp.net-mvc multi-tenant aspnetboilerplate

我正在开发一个 Multi-Tenancy 应用程序,并且希望能够选择删除租户。然而,这似乎没有人们想象的那么微不足道。

我的目标是删除数据库中各处对租户的所有引用。我知道租户是软删除的,但我不想让我的数据库填满旧的无意义的数据,所以我尝试禁用软删除过滤器。

这是我试过的一些代码:

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 表。我还试图从所有其他表中删除。例如 AbpSettings 和 AbpLanguages。我完全不清楚该怎么做——“经理”不包含任何删除功能。

我尝试为这些实体创建 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.

来自 Disable SoftDelete for AbpUserRole 上的问题:

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.

注入(inject) 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。

关于asp.net-mvc - 如何彻底删除租户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49011215/

相关文章:

c# - 与linq的连接查询不同

google-app-engine - 谷歌应用引擎 : Is it possible to allow users to add their own domain for a Multitenancy app?

asp.net - 如何注册通用存储库的依赖?

aspnetboilerplate - EF Core 迁移与 aspnetboilerplate 在哪里触发 context.Database.Migrate();

javascript - 在没有 ajax 调用的情况下使用带有 @HTML.BeginForm 的 formdata append

c# - 如何从我创建的 Html Helper 类中获取完整的 Url?

asp.net-mvc - 在 View 中创建 DropDownListFor 项目

mysql - MySQL 应用程序中的安全 Multi-Tenancy

c# - ASP.NET Core 6 中租户特定的容器

c# - 如何支持用户名unicode?