[英]A quick test shows big difference comparing EF core to ADO.Net
I am reading that the EF core performance is improved and is almost as fast as ADO.NET and decided to create a simple test.我读到 EF 核心性能得到了改进,几乎与 ADO.NET 一样快,因此决定创建一个简单的测试。 The results I got, show huge difference.我得到的结果,显示出巨大的差异。 ADO.NET was 10 times faster. ADO.NET 快了 10 倍。
I am posting here the code I used to test.我在这里发布了我用来测试的代码。 Is there something wrong with my test?我的测试有问题吗? Steps taken:采取的步骤:
Reverse engineer a database using Scaffold-DbContext.使用 Scaffold-DbContext 对数据库进行逆向工程。 Get one record from one table:从一张表中获取一条记录:
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;
}
Here is the GetDisbursements code这是 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;
}
Here are the results:结果如下:
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...
Where am I wrong in my comparison?我的比较哪里错了? Thanks谢谢
Following the remarks of TheGenral "first call to dbContext", I added a query to another table before the comparison and now I am getting close enough results.根据 TheGenral “第一次调用 dbContext”的评论,我在比较之前向另一个表添加了一个查询,现在我得到了足够接近的结果。 Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.