- 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)
-
0
Hi,
Can you try cal the
await UnitOfWorkManager.Current.SaveChangesAsync();
afterawait _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(); }
-
0
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. -
0
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
-
0
Hi,
Can you share a project that can reproduce the problem with me? shiwei.liang@volosoft.com. I will check it. thanks
-
0
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 ofcustomersAppService
class, It's actually a dynamic HTTP proxy, making requests using HttpClient. see: https://docs.abp.io/en/abp/latest/API/Dynamic-CSharp-API-ClientsSo, you need to change the current tenant in the AppService instead of UI code.