我正在阅读EF核心性能得到了改善,几乎与ADO.NET一样快,因此决定创建一个简单的测试。我得到的结果显示出巨大的差异。 ADO.NET的速度提高了10倍。
我在这里发布了我曾经测试过的代码。我的考试有问题吗? 采取的步骤:
使用Scaffold-DbContext对数据库进行反向工程。 从一张表中获得一条记录:
using (var db = new TestFECore.Models.AchieveDB_EncryptContext())
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var disbursement = db.Disbursement
.Where(c => c.CashOutid > 999 && c.CashOutid < 2001)
.First();
Console.WriteLine(disbursement.DisbursementId.ToString());
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time elapsed:" + elapsedMs.ToString());
var dis = new TestFECore.Models.Disbursement();
var ds = new DataService();
watch = System.Diagnostics.Stopwatch.StartNew();
dis = ds.GetDisbursements();
Console.WriteLine("Cashoutid from regular ado.net " + dis.DisbursementId.ToString());
watch.Stop();
elapsedMs = watch.ElapsedMilliseconds;
}
这是GetDisbursements代码
public Models.Disbursement GetDisbursements()
// public List<Models.Disbursement> GetDisbursements()
{
Models.Disbursement disbursement = new Models.Disbursement();
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection("Data Source=*******; etc"))
{
string commandString = @" The exact SQL created by the EF ";
SqlCommand command =
new SqlCommand(commandString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
disbursement.DisbursementId = (int)reader["DisbursementId"];
}
reader.Close();
}
return disbursement;
}
以下是结果:
Core 3.0 Testing!
Querying Disbursments
1498
Time elapsed:2138
Using regular ado.net
Cashoutid from regular ado.net 1498
Time elapsed:144
Press enter to continue...
我在比较中哪里出错了? 谢谢
答案 0 :(得分:0)
按照TheGenral的“对dbContext的首次调用”的说明,在比较之前,我向另一个表添加了一个查询,现在我获得了足够的结果。谢谢