gpt4 book ai didi

c# - 此 EF 查询中有多少个数据库调用?

转载 作者:行者123 更新时间:2023-12-04 10:12:11 27 4
gpt4 key购买 nike

我正在接管某人的代码并遇到了这个 Entity Framework 查询,但我从未见过它以这种方式完成。不是每次调用 .FirstOrDefault 时都会进行数据库查询吗?那么在这种情况下,会有 4 个数据库查询意味着 5 个打开/关闭数据库连接?只是想了解这是否是一种无效的方法,这对我来说似乎是这样。

var record = from e in ctx.bio_employee.Where(x => x.emp_id == emp_id)
select new
{
ta.ta_system
,ta.bio_consent_flag
,e.bio_consentform_rid
};


if (record.FirstOrDefault() != null)
{
vm.TASystem = record.FirstOrDefault().ta_system;
vm.bio_consent_flag = record.FirstOrDefault().bio_consent_flag == null ? "N" : record.FirstOrDefault().bio_consent_flag.Trim().ToUpper();
vm.employee_bio_consentform_rid = record.FirstOrDefault().bio_consentform_rid;
}

最佳答案

尽管连接池将重用单个连接,但它会分别执行 4 或 5 次相同的查询。应该

var query = from e in ctx.bio_employee.Where(x => x.emp_id == emp_id)
select new
{
ta.ta_system
,ta.bio_consent_flag
,e.bio_consentform_rid
};

var result = query.FirstOrDefault();

if (result != null)
{
vm.TASystem = result .ta_system;
vm.bio_consent_flag = result .bio_consent_flag == null ? "N" : result .bio_consent_flag.Trim().ToUpper();
vm.employee_bio_consentform_rid = result.bio_consentform_rid;
}

关于c# - 此 EF 查询中有多少个数据库调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61273673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com