ef核心导航属性无法加载

时间:2020-02-14 11:38:26

标签: entity-framework .net-core linq-to-entities ef-core-2.2 navigation-properties

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

在MapAddress方法中,尽管我有

,但地址对象的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
        };
    }

3 个答案:

答案 0 :(得分:2)

使用此

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

答案 1 :(得分:0)

更改然后包含表达式。

在dbContext.CorporateInfo.Include(a => a.Address)中加入c.ThenInclude(a => a.Address.Country)...

答案 2 :(得分:0)

我做到了这一点,引入了另一种中间方法,该方法接受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();

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

我想的是,当我在投影内请求C.Address时,它只是在查询生成时忽略了ThenInclude命令。