entity framework與ADO.NET 2.0效率
拿entity framework與原始的ADO.NET 2.0對比看看。
我一直很猶豫該不該在專案使用entity framework,因為用ADO.NET 2.0,搭配codesmith開發效率也是挺高的,並且ADO.NET對團隊的素質要求相對沒entity framework那麼高,在公司資源有限,無法招攬些開發能力較高的程式設計師的情況下,是否該用entity framework呢?
這篇文章沒有要得出什麼結論,純粹是我學習entity framework的過程中的一些試驗筆記。
建了個表,對應的實體:
public class User
{
[Key]
public int UserId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
1.執行:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
User u = new User { Name = "test3", Address = "some address" };
Console.WriteLine(DateTime.Now.ToLongTimeString());
//mc.Configuration.AutoDetectChangesEnabled = false;
mc.Users.Add(u);
//mc.Configuration.AutoDetectChangesEnabled = true;
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
看起來,Add方法比較耗時;
2.把AutoDetectChangesEnabled設定為false執行:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
User u = new User { Name = "test3", Address = "some address" };
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.Configuration.AutoDetectChangesEnabled = false;
mc.Users.Add(u);
mc.Configuration.AutoDetectChangesEnabled = true;
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
看起來效果差不多。
3.如果insert 10000行資料:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
try
{
Console.WriteLine(DateTime.Now.ToLongTimeString());
//mc.Configuration.AutoDetectChangesEnabled = false;
for (int i = 0; i < 10000; i++)
{
User u = new User { Name = "test" + i.ToString(), Address = "some address" };
mc.Users.Add(u);
}
}
finally
{
//mc.Configuration.AutoDetectChangesEnabled = true;
}
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
看起來,Add方法特別耗時,注意:如果這裡使用mc.Users.AddRange(XXX)去insert10000行資料,速度就會比較快;
4.把AutoDetectChangesEnabled設定為false執行insert 10000行資料:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
try
{
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.Configuration.AutoDetectChangesEnabled = false;
for (int i = 0; i < 10000; i++)
{
User u = new User { Name = "test" + i.ToString(), Address = "some address" };
mc.Users.Add(u);
}
}
finally
{
mc.Configuration.AutoDetectChangesEnabled = true;
}
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
速度快很多。
5.普通ADO.NET insert 10000行資料:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
string sql = "insert into Users(Name,Address) values (@Name,@Address) ";
Console.WriteLine(DateTime.Now.ToLongTimeString());
for (int i = 0; i < 10000; i++)
{
System.Data.SqlClient.SqlParameter[] paras = {
new System.Data.SqlClient.SqlParameter("@Name ", System.Data.SqlDbType.NVarChar),
new System.Data.SqlClient.SqlParameter("@Address ", System.Data.SqlDbType.NVarChar)
};
paras[0].Value = "test" + i.ToString();
paras[0].Value = "some address";
SqlHelper.sqlToExecute(sql, paras);
}
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
速度確實是最快的。
以上是insert的,現在試試update 10000 行資料:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
try
{
var lst = mc.Users.ToList();//資料庫有10000行資料
Console.WriteLine(DateTime.Now.ToLongTimeString());
//mc.Configuration.AutoDetectChangesEnabled = false;
foreach (var a in lst)
{
a.Address = a.Address + "1000";
}
}
finally
{
//mc.Configuration.AutoDetectChangesEnabled = true;
}
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
看起來,update的速度是很讓人滿意的。
那麼delete呢?
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
try
{
var lst = mc.Users.ToList();
Console.WriteLine(DateTime.Now.ToLongTimeString());
//mc.Configuration.AutoDetectChangesEnabled = false;
foreach (var a in lst)
{
mc.Users.Remove(a);
}
//如果直接用mc.Users.RemoveRange(lst);,也會快的
}
finally
{
//mc.Configuration.AutoDetectChangesEnabled = true;
}
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
看起來特別慢;
然而如果加上mc.Configuration.AutoDetectChangesEnabled = false,速度就上來了:
static void Main(string[] args)
{
using(MyContext mc = new MyContext())
{
try
{
var lst = mc.Users.ToList();
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.Configuration.AutoDetectChangesEnabled = false;
foreach (var a in lst)
{
mc.Users.Remove(a);
}
}
finally
{
mc.Configuration.AutoDetectChangesEnabled = true;
}
Console.WriteLine(DateTime.Now.ToLongTimeString());
mc.SaveChanges();
Console.WriteLine(DateTime.Now.ToLongTimeString());
}
}
相關文章
- Entity Framework Core 2.0 入門Framework
- Entity Framework(1)Framework
- Entity Framework版本歷史概覽Framework
- 重新認識了下Entity FrameworkFramework
- 關於對Entity Framework Core3.1的理解與總結Framework
- 【譯】Announcing Entity Framework Core 5.0 Preview 5FrameworkView
- Entity Framework Core 2.1,新增種子資料Framework
- Entity Framework Core中的併發處理Framework
- 在Entity Framework中使用儲存過程Framework儲存過程
- .NET Framework 4.8 Web API+Entity Framework(實現增刪改查)FrameworkWebAPI
- 【視訊】Entity Framework Core 2.* 入門教程Framework
- 在Visual Studio 2013中配置Entity Framework使用MySQLFrameworkMySql
- ado.net 與lot
- ADO.NET 1.1和2.0事務的區別
- Entity Framework使用DBContext實現增刪改查示例FrameworkContext
- 10、Entity Framework Core 3.1入門教程-執行原生SQLFrameworkSQL
- 在WebApi專案裡使用MiniProfiler並且分析 Entity Framework CoreWebAPIFramework
- 1、Entity Framework Core 3.1入門教程-概述和準備工作Framework
- .NET Core學習筆記(8)——Entity Framework Core之Database First筆記FrameworkDatabase
- .NET Core學習筆記(9)——Entity Framework Core之Code First筆記Framework
- ASP.Net Core專案在Mac上使用Entity Framework Core 2.0進行遷移可能會遇到的一個問題.ASP.NETMacFramework
- Entity Framework 實體載入外來鍵屬性的問題Framework
- Entity Framework Core 7中高效地進行批次資料插入Framework
- Entity Framework 更新失敗,除錯後發現是AsNoTracking的原因Framework除錯
- ADO與ADO.NET 的區別
- 2、Entity Framework Core 3.1入門教程-建立資料庫和遷移Framework資料庫
- .NET 5學習筆記(10)——Entity Framework Core之切換SQLServer和SQLite筆記FrameworkServerSQLite
- 張高興的 Entity Framework Core 即學即用:(一)建立第一個 EF Core 應用Framework
- ADO.NET 總結
- Entity Framework 4 in Action讀書筆記——第六章:理解實體的生命週期Framework筆記
- Entity Framework Core 中使用倉庫和工作單元事務,服務層和控制器Framework
- Visual Studio 2022 安裝低版本的 .Net Framework(2.0~4.5)Framework
- 簡述ADO.NET(一)
- ADO.NET入門教程之Command物件與資料檢索物件
- Dynamics CRM Error- Creating Entity with an invalid parent. Entity: EmailErrorAI
- 質量與效率
- Ado.NET基礎必備
- 13本熱門書籍免費送!(Python、SpingBoot、Entity Framework、Ionic、MySQL、深度學習、小程式開發等)PythonbootFrameworkMySql深度學習