0
mayureshpisal created
Hi ,
I am facing issue to get data from OData service Below is My scenario
I have two DBContext
- MainDBContext:- Which Contains all Users and Tenant Information
- TenantDbContext :- Which contains tenant Specific Entity
A user wants to load data into excel using OData service for that reason I have created one OData service .first user sends a request using username and password. I Authenticate that user and find the tenant of that user. when I Get the Tenant Then I initialize TenantDbContext with TenantConnection String Below is the code.
public class TenantEntityController: AbpODataEntityController<TenantEntity> { IRepository<TenantEntity> _repository; public readonly TenantManager _tenantManager;
public TenantEntityController(IRepository<TenantEntity> repository,TenantManager TenantManager)
: base(
repository)
{
_repository = repository;
_tenantManager = TenantManager;
}
[EnableQuery(PageSize =400)]
[UnitOfWork]
public override IQueryable<TenantEntity> Get()
{
string DBConnectionString = string.Empty;
SqlConnectionStringBuilder Builder = null;
int TenantID= (int)UnitOfWorkManager.Current.GetTenantId();
if (TenantID > 0)
{
Tenant _tenants = _tenantManager.Tenants.Where(c=>c.Id==TenantID).FirstOrDefault();
string QuotedDBConnectionString = _tenants.ConnectionString.Replace("\"", "");
Builder = new SqlConnectionStringBuilder(QuotedDBConnectionString);
}
using (TenantDbContext db = new TenantDbContext(Builder.ToString()))
{
return db.TenantEntity;**//Here no exception but output is not Comming**
}
}
}
}
So do you have any idea or any other solution to achieve this ???
1 Answer(s)
-
0
Hi,
You can do it like this,
using(UnitOfWorkManager.Current.SetTenantId(null)){ var tenantId = "get tenant id here"; using(UnitOfWorkManager.Current.SetTenantId(tenantId)){ //get records of tenant here... } }