在 abp.io 框架中插入具有 TenantId 的实体

Inserting an entity with TenantId in abp.io framework

我下面有一个实体,当我创建时,用 TenantId null 插入。是否需要使用CurrentTenant.Id手动设置TenantId

public class Hall : AuditedAggregateRoot<Guid>, IMultiTenant
{
    public string Name { get; set; }
    public string Location { get; set; }
    public string Explanation { get; set; }
    public Guid? TenantId { get; set; }
}

如果不继承AsyncCrudAppService,那么需要自己设置TenantId

protected void TryToSetTenantId(TEntity entity)
{
    var tenantId = CurrentTenant.Id;

    if (!tenantId.HasValue)
    {
        return;
    }

    var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId));

    if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
    {
        propertyInfo.SetValue(entity, tenantId, null);
    }
}

ABP 框架 (abp.io) 不限制您可能希望在租户范围内创建主机或其他租户实体的情况。

参考文献: