EF-запрос попадает в базу данных более одного раза - PullRequest
0 голосов
/ 10 января 2019

возможно это простая проблема, но я довольно новичок в EF, так что:

У меня есть этот запрос:

                Dim InvoicesQuery = (From i As Invoice In dbContext.Invoices.AsNoTracking() Where i.InvoiceNumber = -1
                             Select New InvoicesWithDetails With {
                                        .InvoiceDetails = i.InvoicesDetails,
                                        .InvoiceDetailsUnmerged = i.InvoicesDetailsUnmergeds,
                                        .Examination = Nothing,
                                        .Applicant = Nothing,
                                        .InvoiceNumber = i.InvoiceNumber,
                                        .InvoiceYear = i.InvoiceYear,
                                        .DocCode = i.DocCode,
                                        .CompanyId = i.CompanyId,
                                        .InvoiceDate = i.InvoiceDate,
                                        .NetAmount = i.NetAmount,
                                        .AmountPaid = i.AmountPaid,
                                        .AmountToPay = i.AmountToPay,
                                        .VAT = i.VAT,
                                        .AdditionalTax = i.AdditionalTax,
                                        .Status = i.Status,
                                        .Amount = i.Amount,
                                        .ExaminationId = i.ExaminationId,
                                        .ReceiverName = If(i.OtherReceiverName Is Nothing, i.ReceiverName, i.OtherReceiverName),
                                        .ReceiverAddress = If(i.OtherReceiverAddress Is Nothing, i.ReceiverAddress, i.OtherReceiverAddress),
                                        .ReceiverCity = If(i.OtherReceiverCity Is Nothing, i.ReceiverCity, i.OtherReceiverCity),
                                        .ReceiverTaxCode = If(i.OtherReceiverTaxCode Is Nothing, i.ReceiverTaxCode, i.OtherReceiverTaxCode),
                                        .ReceiverCompanyTaxCode = i.ReceiverCompanyTaxCode,
                                        .ReceiverZipCode = If(i.OtherReceiverZipCode Is Nothing, i.ReceiverZipCode, i.OtherReceiverZipCode),
                                        .IsCreditNote = i.IsCreditNote,
                                        .HasCreditNote = i.HasCreditNote,
                                        .FixedAdditionalAmountOnInvoice = i.FixedAdditionalAmountOnInvoice,
                                        .CashFlowPaymentModeId = If(i.CashFlowPaymentModeId IsNot Nothing, i.CashFlowPaymentModeId.Trim, Nothing),
                                        .AgreementDeductible = i.AgreementDeductible,
                                        .IsPatientInvoiceRecipient = i.IsPatientInvoiceRecipient,
                                        .IsApplicantInvoiceRecipient = i.IsApplicantInvoiceRecipient,
                                        .IsDoctorInvoiceRecipient = i.IsDoctorInvoiceRecipient,
                                        .DoctorId = If(i.DoctorId IsNot Nothing, i.DoctorId, Nothing),
                                        .Doctor = Nothing,
                                        .CreditNoteParentInvoice = i.CreditNoteParentInvoice,
                                        .CreditNoteParentInvoiceYear = i.CreditNoteParentInvoiceYear,
                                        .IsDeferredInvoice = i.IsDeferredInvoice,
                                        .IsExtracted = If(i.IsExtracted IsNot Nothing, i.IsExtracted, False),
                                        .IsExportedToHOST = If(i.IsExportedToHOST IsNot Nothing, i.IsExportedToHOST, False),
                                        .ApplicantId = i.ApplicantId,
                                        .InvoiceHasVAT = i.InvoiceHasVAT,
                                        .VAT_Percentage = i.VAT_Percentage,
                                        .IssuedBy = i.IssuedBy,
                                        .MaskedCounter = i.MaskedCounter,
                                        .DocTypeId = i.DocTypeId,
                                        .OtherReceiverName = i.OtherReceiverName,
                                        .OtherReceiverAddress = i.OtherReceiverAddress,
                                        .OtherReceiverCity = i.OtherReceiverCity,
                                        .OtherReceiverTaxCode = i.OtherReceiverTaxCode,
                                        .OtherReceiverZipCode = i.OtherReceiverZipCode,
                                        .CashFlows = i.CashFlows}).ToList

InvoicesWithDetails - класс, который наследует Invoice; Счет-фактура является сущностью БД

После этого вызова я перебираю результаты примерно так:

            For Each i As InvoicesWithDetails In InvoicesQuery.Where(Function(iwd) iwd.CompanyDescription Is Nothing And iwd.CompanyId IsNot Nothing)
            i.CompanyDescription = ReturnCompanyDescription(i.CompanyId)
        Next

Для каждого цикла в цикле вызывается база данных. Я подумал, что после .ToList при первом вызове я мог бы повторять результаты без вызова базы данных.

Что не так? Заранее спасибо

Хорошо, я работал над этим и попробовал другой подход:

Это новый запрос, нацеленный на объект Invoice:

                            Dim InvoicesQuery As List(Of Invoice)
                        InvoicesQuery = (From i As Invoice In dbContext.Invoices.AsNoTracking Where
                                 i.InvoiceDate >= FromDate And i.InvoiceDate <= ToDate).ToList

После этого я создаю новый экземпляр InvoiceWithDetails и устанавливаю все свойства следующим образом:

            For Each i As Invoice In InvoicesQuery
            Dim newInvoiceWithDetails As New InvoicesWithDetails
            With newInvoiceWithDetails
                .InvoiceDetails = i.InvoicesDetails
                .InvoiceDetailsUnmerged = i.InvoicesDetailsUnmergeds
                .Examination = Nothing
                .Applicant = Nothing
                .InvoiceNumber = i.InvoiceNumber
                .InvoiceYear = i.InvoiceYear
                .DocCode = i.DocCode
                .CompanyId = i.CompanyId
                .InvoiceDate = i.InvoiceDate
                .NetAmount = i.NetAmount
                .AmountPaid = i.AmountPaid
                .AmountToPay = i.AmountToPay
                .VAT = i.VAT
                .AdditionalTax = i.AdditionalTax
                .Status = i.Status
                .Amount = i.Amount
                .ExaminationId = i.ExaminationId
                .ReceiverName = If(i.OtherReceiverName Is Nothing, i.ReceiverName, i.OtherReceiverName)
                .ReceiverAddress = If(i.OtherReceiverAddress Is Nothing, i.ReceiverAddress, i.OtherReceiverAddress)
                .ReceiverCity = If(i.OtherReceiverCity Is Nothing, i.ReceiverCity, i.OtherReceiverCity)
                .ReceiverTaxCode = If(i.OtherReceiverTaxCode Is Nothing, i.ReceiverTaxCode, i.OtherReceiverTaxCode)
                .ReceiverCompanyTaxCode = i.ReceiverCompanyTaxCode
                .ReceiverZipCode = If(i.OtherReceiverZipCode Is Nothing, i.ReceiverZipCode, i.OtherReceiverZipCode)
                .IsCreditNote = i.IsCreditNote
                .HasCreditNote = i.HasCreditNote
                .FixedAdditionalAmountOnInvoice = i.FixedAdditionalAmountOnInvoice
                .CashFlowPaymentModeId = If(i.CashFlowPaymentModeId IsNot Nothing, i.CashFlowPaymentModeId.Trim, Nothing)
                .AgreementDeductible = i.AgreementDeductible
                .IsPatientInvoiceRecipient = i.IsPatientInvoiceRecipient
                .IsApplicantInvoiceRecipient = i.IsApplicantInvoiceRecipient
                .IsDoctorInvoiceRecipient = i.IsDoctorInvoiceRecipient
                .DoctorId = If(i.DoctorId IsNot Nothing, i.DoctorId, Nothing)
                .Doctor = Nothing
                .CreditNoteParentInvoice = i.CreditNoteParentInvoice
                .CreditNoteParentInvoiceYear = i.CreditNoteParentInvoiceYear
                .IsDeferredInvoice = i.IsDeferredInvoice
                .IsExtracted = If(i.IsExtracted IsNot Nothing, i.IsExtracted, False)
                .IsExportedToHOST = If(i.IsExportedToHOST IsNot Nothing, i.IsExportedToHOST, False)
                .ApplicantId = i.ApplicantId
                .InvoiceHasVAT = i.InvoiceHasVAT
                .VAT_Percentage = i.VAT_Percentage
                .IssuedBy = i.IssuedBy
                .MaskedCounter = i.MaskedCounter
                .DocTypeId = i.DocTypeId
                .OtherReceiverName = i.OtherReceiverName
                .OtherReceiverAddress = i.OtherReceiverAddress
                .OtherReceiverCity = i.OtherReceiverCity
                .OtherReceiverTaxCode = i.OtherReceiverTaxCode
                .OtherReceiverZipCode = i.OtherReceiverZipCode
                .CashFlows = i.CashFlows
            End With
            FinalList.Add(newInvoiceWithDetails)

Для каждого цикла и каждого значения свойства вызывается база данных

Опять же, что не так?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...