ef查询“随机”返回没有结果或正确的结果
使用内部联接调用简单的 select 会导致返回 2 种不同的情况。
它要么返回包含所有数据的集合,要么不返回任何数据。 这在本地运行时可以 100% 工作(因此在调试代码时无法跟踪它)。
运行 sql profiler 我无法找出差异(执行两个查询每次都会返回正确的数据)。
甚至使用了DbContextDatabase.Log => eventLog.WriteEntry() 来记录它。
我可以看到查询和所有参数都已正确传递。唯一的区别是日志(就在连接关闭之前) -- 在 xx 毫秒内完成,结果为:SqlDataReader(返回数据时)与 -- 在 0 毫秒内完成,结果:SqlDataReader(没有返回数据)
如果需要,我可以提供更多详细信息
: 日期时间 dtDate; int compId;
出去: 列表
询问:
result = (from x in Database.Table_1.AsNoTracking()
.Where(a => dtDate < (a.EndDate ?? DateTime.UtcNow)&& a.StartDate <= dtDate && a.CompanyId == compId)
from y in Database.Table_2
.Where(b => b.Id == x.CId && dtDate < (b.EndDate ?? DateTime.UtcNow)&& dtDate > b.StartDate && b.SiteID == compId)
select new Table_1DTO
{
ID = x.ID,
CompanyId = x.CompanyId,
col1 = x.col1,
col2 = x.col2,
col3 = x.col3,
col4 = x.col4,
col5 = x.col5,
col6 = x.col6,
CId = x.CId,
CategoryName = y.CategoryName
}).ToList();
Calling simple select with inner join causes to return 2 different scenarios.
Either it returns the set with all data or returns no data.
This will work 100% when running locally (so unable to trace it while debugging the code).
Running sql profiler I was unable to figure out the difference (executing both queries will return correct data each time).
Even used DbContextDatabase.Log => eventLog.WriteEntry() to log it.
I can see the query and all the params passed ok. The only difference is log (right before connection closed)
-- Completed in xx ms with result: SqlDataReader (when data are returned) vs
-- Completed in 0 ms with result: SqlDataReader (with no data returned)
I can provide more details if needed
IN:
Datetime dtDate;
int compId;
OUT:
List<Table_1DTO>
query:
result = (from x in Database.Table_1.AsNoTracking()
.Where(a => dtDate < (a.EndDate ?? DateTime.UtcNow)&& a.StartDate <= dtDate && a.CompanyId == compId)
from y in Database.Table_2
.Where(b => b.Id == x.CId && dtDate < (b.EndDate ?? DateTime.UtcNow)&& dtDate > b.StartDate && b.SiteID == compId)
select new Table_1DTO
{
ID = x.ID,
CompanyId = x.CompanyId,
col1 = x.col1,
col2 = x.col2,
col3 = x.col3,
col4 = x.col4,
col5 = x.col5,
col6 = x.col6,
CId = x.CId,
CategoryName = y.CategoryName
}).ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论