Repository navigation
Description
I have query like this, result1 success get data but result2 stuck(not throw exception and not get result).
var trx = context.Adjustments.Select(x => new TransactionRef
{
Id = x.Id,
Source = x.IsPositive ? UserActionSource.AdjustmentPlus : UserActionSource.AdjustmentMinus,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = x.CostCenterId
}).Union(context.SupplierDownPayments.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.SupplierDownPayment,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.PurchaseRequisitions.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.PurchaseRequisition,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.PurchaseOrders.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.PurchaseOrder,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.ReceivedItems.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.ReceivedItem,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
}))
.Union(context.Purchases.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.Purchase,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.PurchaseReceivings.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.PurchaseReceiving,
RefCode = x.Code,
BranchId = null,
CostCenterId = null
})).Union(context.PurchaseReturns.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.PurchaseReturn,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.JournalVouchers.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.JournalVoucher,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.OtherDeposits.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.OtherDeposit,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.OtherPayments.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.OtherPayment,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.WarehouseTransfers.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.WarehouseTransfer,
RefCode = x.Code,
BranchId = null,
CostCenterId = null
})).Union(context.ItemTransferIns.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.ItemTransferIn,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
})).Union(context.ItemTransferOuts.Select(x => new TransactionRef
{
Id = x.Id,
Source = UserActionSource.ItemTransferOut,
RefCode = x.Code,
BranchId = x.BranchId,
CostCenterId = null
}));
var result1= await (from i in trx
select new
{
i.Id
}).ToListAsync(cancellationToken);
var result2 = await (from i in trx
select new
{
i.Id,
IsA = (from ah in context.AccountHistories where i.Id == ah.RefId select ah.Id).Count() > 0
}).ToListAsync(cancellationToken);how to fix it?
Further technical details
EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.4
Activity
suriyadi15 commentedon Jan 29, 2020
result2has the result if thetrxuse 2Unionajcvickers commentedon Jan 29, 2020
@suriyadi15 Can you look in the logs and post the SQL generated from the query?
maumar commentedon Jan 29, 2020
@suriyadi15 also please post code for all your entities and the contents of OnModelCreating method in the DbContext
suriyadi15 commentedon Jan 30, 2020
sql generated from
result2is nothing, when i debug and break point in result2 and pressf10, nothing response and don't throw exception. my request from browser is still pending.suriyadi15 commentedon Jan 30, 2020
This is one sample entity & configuration code
ajcvickers commentedon Jan 30, 2020
@roji Thoughts?
roji commentedon Jan 30, 2020
The big LINQ query with all the Unions is assigned to variable
trxwhich is not evaluated/executed (there is no ToList or anything). Then, the two other queries producing result1 and result2 query from something calledawhich isn't shown.Is it possible to have the runnable code actually being executed? I'm afraid there's something missing in your sample which could explain the issue.
suriyadi15 commentedon Jan 31, 2020
I apologize for the writing error above. I have fixed the code above, the variable of
ashould betrx.should be
from i in trxroji commentedon Jan 31, 2020
@suriyadi15 thanks. The SQL produced for result1 seems correct, could you please set your logging level to
Debugand see if an SQL query comes out for result2 as well? The log event with the SQL you're seeing is "Executed DbCommand", which is logged on levelInformation(the default) and which only occurs after the query has been executed. In your situation, if EF Core is sending SQL to the database that causes the database to block, you wouldn't see any SQL logged unless you enableDebugfor the "Executing DbCommand" event.I don't have enough information yet to know exactly what's going on, but I suspect the second query simply is taking too long to execute for the database. This could be because of a massive amount of data in AccountHistories, or missing indexes that would help the query run more quickly. So I'd suggest trying to run the same program against an empty development/preproduction database to see the results. If there really is something wrong with EF Core, the query should also hang there; otherwise it should execute quickly, pointing more to a data/indexes/database-side problem.
suriyadi15 commentedon Feb 1, 2020
I have tried to set Logging level to Debug and empty database. but i don't see logging for
result2here is the output debug:
when i try running it on sql server, it runs normally, this is the query:
Expected results from 'result2' should be like the query above. Is there an error with my code?
This is the output debug if the
Unionis only one:ajcvickers commentedon Feb 8, 2020
Pinging @roji as requested.
[-]Query not working and not throw exception[/-][+]Exponential hash code calculation in ReplacingExpressionVisitor[/+]8 remaining items
ReplacingEV uses two arrays instead of dictionary (#19858)
roji commentedon Feb 10, 2020
Reopening for servicing in 3.1
ReplacingEV uses two arrays instead of dictionary (#19858)
roji commentedon Feb 10, 2020
Using #19867 to track the servicing fix.
ReplacingEV uses two arrays instead of dictionary (#19858)