Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

TenantID and joins #231


User avatar
0
balikpinoy created

Hi! Thanks for this great piece of work.

I'm having some issue doing a two table query and it looks like the tenantid is not being injected correctly.

Two tables, homeowners and property joined on propertyid..I used glimpse to look at the sql being generated by EF.

Here's a simple homeowner query with the tenantid correctly filtered.

FROM [dbo].[Homeowners] AS [Var_3] WHERE (([Var_3].[TenantId] = 2 /* @DynamicFilterParam_5 /) OR (NULL / @DynamicFilterParam_6 / IS NOT NULL)) AND (([Var_3].[IsDeleted] = False / @DynamicFilterParam_1 /) OR (NULL / @DynamicFilterParam_2 */ IS NOT NULL)) Name Value Type Size @DynamicFilterParam_5 2 Int32 0 @DynamicFilterParam_6 NULL Boolean 0 @DynamicFilterParam_1 false Boolean 0

My other query

FROM [dbo].[Properties] AS [Var_4] WHERE (([Var_4].[TenantId] = 0 /* @DynamicFilterParam_5 /) OR (NULL / @DynamicFilterParam_6 / IS NOT NULL)) AND (([Var_4].[IsDeleted] = False / @DynamicFilterParam_1 /) OR (NULL / @DynamicFilterParam_2 / IS NOT NULL)) ) AS [Extent1] INNER JOIN (SELECT [Var_5].[Id] AS [Id], [Var_5].[TenantId] AS [TenantId], [Var_5].[PersonID] AS [PersonID], [Var_5].[PropertyID] AS [PropertyID], [Var_5].[StartDate] AS [StartDate], [Var_5].[OwnerType] AS [OwnerType] FROM [dbo].[Homeowners] AS [Var_5] WHERE (([Var_5].[TenantId] = 0 / @DynamicFilterParam_5 /) OR (NULL / @DynamicFilterParam_6 / IS NOT NULL)) AND (([Var_5].[IsDeleted] = False / @DynamicFilterParam_1 /) OR (NULL / @DynamicFilterParam_2 / IS NOT NULL)) ) AS [Extent2] ON [Extent1].[Id] = [Extent2].[PropertyID] WHERE ([Extent2].[PersonID] = 22 / @p__linq__0 */)

Am I doing anything wrong? Here's the original query

            var model = from p in db.Properties 
                        join h in db.Homeowners 
                            on p.Id equals h.PropertyID
                        where h.PersonID == pId  
                        select new PropertyViewModel()
                        {
                            ID = p.Id,
                            Village = p.Village,
                            HomeownerID = h.Id, ...
                        };

I also tried using repository.GetAll() instead of db but I kept getting dbContext has been disposed error.

Thanks !


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

    Hi,

    For "dbContext has been disposed" error, see unit of work document: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocRepositoryGetAll">http://www.aspnetboilerplate.com/Pages/ ... toryGetAll</a>

    Have you implemented IMustHaveTenant interface for your entities?

  • User Avatar
    0
    balikpinoy created

    Yes..I did implement the IMusthaveTenant Attributes. As you can see from my example, the filter works for simple queries against one table but not for multiple tables.

    I was able to get around it by creating an application service and disabling the filter thru the CurrentUnitOfWork interface.

    I do have another question as to how to access the CurrentUnitOfWork within my controllers? I've added the attribute to my method and did the correct injection for _unitofworkmanager...but _unitofworkmanager.current always comes up as null.

    Thanks!

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Joins also works for me. I'll check it again.

    For your UOW question, please read UOW documentation: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work">http://www.aspnetboilerplate.com/Pages/ ... it-Of-Work</a>

    In a short answer, make your action virtual, add a UnitOfWork property, inject IUnitOfWorkManager and use IUnitOfWorkManager.Current.

  • User Avatar
    0
    balikpinoy created

    Thanks! I'll try the virtual on the method.

  • User Avatar
    0
    balikpinoy created

    Adding hte Virtual directive worked! And now the tenantid is getting filtered correctly.

    Thanks!