Open Closed

New record does not have TenantId after CurrentTenant.Change #3719


User avatar
0
mat.guthrie@loupa.io created
  • ABP Framework version: v5.3.4
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

Hi,

I'm calling CurrentTenant.Change(existingTenant.Id) within a using statement an attempting to save a new record that belongs to the tenant. Unfortunately, the resulting record in the DB has null in the tenantId. What am I missing?

            // Change Tenant
            using (CurrentTenant.Change(existingTenant.Id))
            {
                // Create New Store
                var newStore = new StoreCreateDto
                {
                    ExternalId = storeName,
                    Name = storeName,
                    Timezone = ecomStore.Timezone 
                };

                await _storesAppService.CreateAsync(newStore);

            }
            

// Simplified Store Entity Below

public class Store : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
    public virtual Guid? TenantId { get; set; }

    [NotNull] public virtual string Name { get; set; }

    [CanBeNull] public virtual string ExternalId { get; set; }

    // Etc
}

Any assistance would be appreciate.

Also, while I'm on the topic of switching tenants, what's the best way to manually locate a tenant (in situations where you can't use a resolver) . Is there a better way than this. Using GetList with a filter seems a but clunky.

            // 1. Load existing Tenant
            var existingTenant = (await _tenantAppService.GetListAsync(new GetTenantsInput { Filter = name})).Items.SingleOrDefault();

Thanks

Mat


5 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Can you try cal the await UnitOfWorkManager.Current.SaveChangesAsync(); after await _storesAppService.CreateAsync(newStore);.

     // Change Tenant
    using (CurrentTenant.Change(existingTenant.Id))
    {
        // Create New Store
        var newStore = new StoreCreateDto
        {
            ExternalId = storeName,
            Name = storeName,
            Timezone = ecomStore.Timezone 
        };
    
        await _storesAppService.CreateAsync(newStore);
        await UnitOfWorkManager.Current.SaveChangesAsync();
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Also, while I'm on the topic of switching tenants, what's the best way to manually locate a tenant (in situations where you can't use a resolver) . Is there a better way than this. Using GetList with a filter seems a but clunky.

    We have a ITenantStore interface, you can use it.

  • User Avatar
    0
    mat.guthrie@loupa.io created

    Hi,

    Ok, I've been some more testing.

    I'm calling CurrentTenant.Change() within an MVC page an it is correctly changing the TenantId.I am then calling the StoresAppService.CreateAsync();

    If I check CurrentTenant.Id within the CreateAsync method it is null.

    Any assistance would be appreciated.

    Mat

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Can you share a project that can reproduce the problem with me? shiwei.liang@volosoft.com. I will check it. thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I found the problem because you are using tiered architecture. that means it is a front-end and back-end separation architecture.

    When you call the CreateAsync method of customersAppService class, It's actually a dynamic HTTP proxy, making requests using HttpClient. see: https://docs.abp.io/en/abp/latest/API/Dynamic-CSharp-API-Clients

    So, you need to change the current tenant in the AppService instead of UI code.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11