ef core navigation property fails to load

Related searches

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

use this

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 = new AddressModel
                                        {
                                          AddressId = c.Address.AddressId,
                                          AddressLine = c.Address.AddressLine1,
                                          City = c.Address.City,
                                          Country = c.Address.Country?.Name,
                                          Province = c.Address.Province,
                                          Division = c.Address.Division,
                                          PhoneNumber = c.Address.PhoneNumber,
                                          PostalCode = c.Address.PostalCode
                                        },                                           
                                        Balance = wd.AvailableBalance,                                          
                                        CreatedOn = wd.CreatedOn
                                    }).FirstOrDefault();

EF Core returns null for a Navigation property, By default, EFCore loads Navigation properties lazily – which means, that unless they're explicitly requested or loaded beforehand, they won't� EF Core will then enable lazy-loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. And here is the provided sample code:


change the then include expression.

join c in dbContext.CorporateInfo.Include(a => a.Address).ThenInclude(a => a.Address.Country)...

Loading Related Data, The short answer is that EF7 does not currently support automatic lazy loading in the same way that EF6 did. I've seen it discussed in a few� The reason is explained in the Loading Related Data section of the EF Core documentation. The first behavior is because EF Core currently does not support lazy loading, so normally you'll get null for navigation properties until you specifically load them via eager or explicit loading. However, the Eager loading section contains the following:


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.

EF Core doesn't lazy-load virtual navigation properties � Issue #3312 , That's because you're simply setting the foreign-key property ( Stage.EmployeeID ) without setting the navigation-property ( Stage.Employee ). Of course, there is a method called “Include” that you can use to instruct Entity Framework to load the property with the specified parameter in the object graph: View the code on Gist . You can add as many includes as you want, as long as you provide a property name.


[SOLVED], Update 2018-11-26: Added the equivalent for EF Core. Instead, I have a solution that automatically loads all navigation properties to the graph. If one of your requirements is performance, this probably won't be the best� EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. For example, in the following entities, the Post.Blog and Blog.Posts navigation properties will be lazy-loaded.


How to automatically load navigation properties in Entity Framework , EF Core will enable lazy-loading for any navigation property that is virtual and in a class that can be inherited. So in our model the Invoice.Customer and Customer.Invoices navigation properties will be lazy-loaded. using (var context = new MyContext ()) { var customers = context.Customers.ToList (); }


However, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. A query is sent to the database each time you try to get data from a navigation property for the first time. Entity Framework Core 1.0 doesn't support lazy loading.