Search code examples
entity-framework.net-corelinq-to-entitiesef-core-2.2navigation-properties

ef core navigation property fails to load


My application is Ef core 2.2 , Lazy loading not enabled.

At MapAddress method address object's Country object is null, although i have

Include(a => a.Address).ThenInclude(a => a.Country)

to load country eagerly.

   var agentWalletResponse = (from wd in dbContext.WalletDetail.Where(w => w.WalletDetailId == agentWalletDetailId)                                        
                                    join c in dbContext.CorporateInfo.Include(a => a.Address).ThenInclude(a => a.Country).Where(d => !d.IsDeleted) on wd.WalletSubscriptionId equals c.OwnerUserId                                                                  
                                    select new AgentWalletResponse()
                                    {

                                        Address = MapAddress(c.Address),                                           
                                        Balance = wd.AvailableBalance,                                          
                                        CreatedOn = wd.CreatedOn
                                    }).FirstOrDefault();

Map address

 protected AddressModel MapAddress(Address address)
    {
        if (address == null)
            return null;

        return new AddressModel
        {
            AddressId = address.AddressId,
            AddressLine = address.AddressLine1,
            City = address.City,
            Country = address.Country?.Name,
            Province = address.Province,
            Division = address.Division,
            PhoneNumber = address.PhoneNumber,
            PostalCode = address.PostalCode
        };
    }

Solution

  • I Achieved this introducing another intermediate method which accept CorporateInfo instead of the address.

    var agentWalletResponse = (from wd in dbContext.WalletDetail
                                        join ws in dbContext.WalletSubscription on wd.WalletSubscriptionId equals ws.WalletSubscriptionId
                                        join ci in dbContext.CorporateInfo.Include(a => a.Address).ThenInclude(a => a.Country).Where(d => !d.IsDeleted) on wd.WalletSubscriptionId equals ci.OwnerUserId
                                        join wc in dbContext.Currency on wd.CurrencyId equals wc.CurrencyId
                                        join ac in dbContext.AgentCommission on ws.WalletSubscriptionId equals ac.WalletSubscriptionId into y
                                        from agentComms in y.DefaultIfEmpty()                                       
                                        join kf in dbContext.KokuFee on ws.WalletSubscriptionId equals kf.WalletSubscriptionId into z
                                        from kf_all in z.DefaultIfEmpty()
                                        where ws.WalletType.Equals(WalletType.Agent) && !kf_all.IsDeleted && wd.WalletDetailId.Equals(agentWalletDetailId)
                                        && !agentComms.IsDeleted
                                        select new AgentWalletResponse
                                        {
                                            Alias = ws.Alias,
                                            Address = MapAddress(ci),
                                            AgentSubscriptionId = ws.WalletSubscriptionId,
                                            AgentWalletDetailId = wd.WalletDetailId,
                                            SubscriptionNo = ws.SubscriptionNo,
                                            Balance = wd.AvailableBalance,
                                            CurrencyCode = wc.CurrencyCode,                                           
                                            Commission = agentComms == null ? 0 : agentComms.Commission,
                                            TopupFee = kf_all == null ? 0 : kf_all.AbsoluteFee,
                                            CreatedOn = wd.CreatedOn
                                        }).FirstOrDefault();
    

     //When use directly ef core fails to load the country of the address. 
        protected AddressModel MapAddress(CorporateInfo corporateInfo)
        {
            return MapAddress(corporateInfo.Address);
        }
    

    What i think is when i request C.Address inside projection, it just ignore my ThenInclude command at query generation.