Entity framework ef核心导航属性无法加载

Entity framework ef核心导航属性无法加载,entity-framework,.net-core,linq-to-entities,ef-core-2.2,navigation-properties,Entity Framework,.net Core,Linq To Entities,Ef Core 2.2,Navigation Properties,我的应用程序是Ef core 2.2,未启用延迟加载 在MapAddress方法中,address对象的Country对象为null,尽管我有 包括(a=>a.Address)。然后包括(a=>a.Country) 热切地装载国家 var agentWalletResponse = (from wd in dbContext.WalletDetail.Where(w => w.WalletDetailId == agentWalletDetailId)

我的应用程序是Ef core 2.2,未启用延迟加载

在MapAddress方法中,address对象的Country对象为null,尽管我有

包括(a=>a.Address)。然后包括(a=>a.Country)

热切地装载国家

   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();
地图地址

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

更改“然后包含”表达式

在dbContext.CorporateInfo.Include(a=>a.Address)中加入c,然后使用Include(a=>a.Address.Country).

使用这个

var agentWalletResponse=(来自dbContext.WalletDetail.Where(w=>w.WalletDetailId==agentwalletdetaild)中的wd)
在dbContext.CorporateInfo.Include(a=>a.Address)中加入c,然后加入(a=>a.Country)。其中wd.WalletSubscriptionId上的(d=>!d.IsDeleted)等于c.OwnerUserId
选择新代理AlletResponse()
{
地址=新的地址模型
{
AddressId=c.Address.AddressId,
AddressLine=c.Address.AddressLine1,
City=c.Address.City,
国家=c.地址.国家?.名称,
省,省,
分部=c.地址.分部,
PhoneNumber=c.Address.PhoneNumber,
PostalCode=c.Address.PostalCode
},                                           
余额=wd.AvailableBalance,
CreatedOn=wd.CreatedOn
}).FirstOrDefault();

我通过引入另一种中间方法实现了这一点,该方法接受CorporateInfo而不是地址

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();


我的想法是,当我在投影中请求C.Address时,它只是在查询生成时忽略我的thenclude命令

谢谢。但是包含意味着它已经在Address对象中了。
 //When use directly ef core fails to load the country of the address. 
    protected AddressModel MapAddress(CorporateInfo corporateInfo)
    {
        return MapAddress(corporateInfo.Address);
    }