Base solution for your next web application
Open Closed

Odata Implementation in 0.10.0 #1429


User avatar
0
mayureshpisal created

Hi ,

I am facing issue to get data from OData service Below is My scenario

I have two DBContext

  1. MainDBContext:- Which Contains all Users and Tenant Information
  2. 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&lt;TenantEntity&gt; repository,TenantManager TenantManager)
        : base(
            repository)
    {
        _repository = repository;
        _tenantManager = TenantManager;
    }

    [EnableQuery(PageSize =400)]
    [UnitOfWork]
    public override IQueryable&lt;TenantEntity&gt; 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)
  • User Avatar
    0
    ismcagdas created
    Support Team

    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...
        }
    }