Не удается загрузить свойство основной навигации - PullRequest
1 голос
/ 14 февраля 2020

Мое приложение Ef core 2.2, отложенная загрузка не включена.

При адресе объекта метода MapAddress объект Country объекта имеет значение null, хотя у меня есть

Включить (a => a. Адрес) .ThenInclude (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 ]

2 голосов
/ 15 февраля 2020

используйте это

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();
0 голосов
/ 20 февраля 2020

Я добился этого, введя еще один промежуточный метод, который принимает 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. Адрес внутри проекции, он просто игнорирует мою команду ThenInclude при генерации запроса.

0 голосов
/ 14 февраля 2020

изменить выражение затем include.

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

...