Base solution for your next web application
Open Closed

Mixing up with data retrieval from host/tenant #2664


User avatar
0
bilalhaidar created

Hi,

I have a set of data that is stored per Tenant.

When I logging in under Host Admin, I try to access that data, I set this:

List<Shelter> shelters;
            shelters = await _shelters.Shelters.OrderBy(s => s.ShelterCode).ToListAsync();

I get all the data. The data is all stored with a TenantId = 1.

So, when I am logging in as Admin Host, the tenant Id should be null.

How come the data is retrieved?

Am I missing something here?

This is the WHERE clause in SQL after using SQL Profiler:

WHERE 
	(
		([TenantId] = 0) OR ( CAST( [TenantId] AS int) IS NULL) OR (1 IS NOT NULL)
	) AND 
		(([IsDeleted] = 0) )

1 IS NOT NULL always true, hence all data are returned. But why do I have that additional filter?

Thanks Bilal


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your entity definition, a screenshot of sample data and code for you app service ?

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Sure, here you go.

    1. Entity (.Core)

    2. AppService (.Application)

    3. Seed method (.EntityFramework)

    4. Table in DB (<a class="postlink" href="https://s21.postimg.org/ra51m36lz/image.png">https://s21.postimg.org/ra51m36lz/image.png</a>)

    5. In a common lookup AppService I use this code:

    public async Task<ReferenceDataDto> GetReferenceTables()
            {
                // Bring Shelters from specific Tenant user is logged in to
                List<Shelter> shelters;
                shelters = await _shelters.Shelters.OrderBy(s => s.ShelterCode).ToListAsync();
    

    This AppService I was calling while logged in as Admin Host.

    Appreciate your assistance.

    Bilal

  • User Avatar
    0
    bilalhaidar created

    I've tried also in the AppService to include this:

    1. SetTenantId

    Notice the TenantId is Null, Shelters are stored all with TenantId = 1, yet they are returned.

    Seems the filter somewhere not being enforced, right?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Normally you don't need SetTenantId. Can you share the code for getting _manager.Shelters in GetShelters method ?

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Here you go:

    public virtual IQueryable<Shelter> Shelters { get { return _repository.GetAll(); } }