EF結合SqlBulkCopy實現高效的批量資料插入 |EF外掛EntityFramework.Extended實現批量更新和刪除
阿新 • • 發佈:2019-01-26
批量插入 (17597條資料批量插入耗時1.7秒)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { using MvcApplication1.Models; using EntityFramework.Extensions; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Diagnostics; public class HomeController : Controller { public ActionResult Index() { Stopwatch sw = new Stopwatch(); //計時器 sw.Start();//開始計時 using (var db = new salesEntities()) { List<location> entitys = db.location.ToList(); //構建集合,到時候會將這個集合資料批量插入到 if (db.Database.Connection.State != ConnectionState.Open) { db.Database.Connection.Open(); //開啟Connection連線 } //呼叫BulkInsert方法,將entitys集合資料批量插入到資料庫的tolocation表中 BulkInsert<location>((SqlConnection)db.Database.Connection, "tolocation", entitys); if (db.Database.Connection.State != ConnectionState.Closed) { db.Database.Connection.Close(); //關閉Connection連線 } } sw.Stop(); //結束計時 string aa = sw.Elapsed.ToString();//批量插入了17597條資料。耗時1.7秒 return View(); } /// <summary> /// 批量插入 /// </summary> /// <typeparam name="T">泛型集合的型別</typeparam> /// <param name="conn">連線物件</param> /// <param name="tableName">將泛型集合插入到本地資料庫表的表名</param> /// <param name="list">要插入大泛型集合</param> public static void BulkInsert<T>(SqlConnection conn, string tableName, IList<T> list) { using (var bulkCopy = new SqlBulkCopy(conn)) { bulkCopy.BatchSize = list.Count; bulkCopy.DestinationTableName = tableName; var table = new DataTable(); var props = TypeDescriptor.GetProperties(typeof(T)) .Cast<PropertyDescriptor>() .Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System")) .ToArray(); foreach (var propertyInfo in props) { bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name); table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType); } var values = new object[props.Length]; foreach (var item in list) { for (var i = 0; i < values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } bulkCopy.WriteToServer(table); } } } }
使用EF擴充套件EntityFramework.Extended 對資料進行批量更新,和批量刪除
首先去nuget上下載EntityFramework.Extended外掛(搜尋:EntityFramework.Extended) 安裝後,在專案中引入using EntityFramework.Extensions; 名稱空間批量更新(17597條資料,批量更新耗時1.69秒)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { using MvcApplication1.Models; using EntityFramework.Extensions; //使用EF的EntityFramework.Extended外掛需要引入此名稱空間 using System.Diagnostics; public class HomeController : Controller { public ActionResult Index() { salesEntities db = new salesEntities(); Stopwatch sw = new Stopwatch(); //計時器 sw.Start(); //呼叫外掛的Update方法進行批量更新(不需要我們手動的db.SaveChanges()了) //db.location.Update(r => new location { version = 123 });//批量將location表裡version欄位資料更新為123 db.tolocation.Where(r => r.locId < 100).Update(c => new tolocation { version = 236 }); //也可以帶條件批量修改 sw.Stop(); string aa = sw.Elapsed.ToString();//批量更新了17597條資料。耗時1.69秒 return View(); } } }
批量刪除(17597條資料,批量刪除耗時1.76秒)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication1.Controllers { using MvcApplication1.Models; using EntityFramework.Extensions; //使用EF的EntityFramework.Extended外掛需要引入此名稱空間 using System.Diagnostics; public class HomeController : Controller { public ActionResult Index() { salesEntities db = new salesEntities(); Stopwatch sw = new Stopwatch(); //計時器 sw.Start(); //呼叫外掛的Delete方法進行批量刪除(不需要我們手動的db.SaveChanges()了) //db.location.Delete(r => r.locId < 100000); db.location.Where(r => r.locId < 10000).Delete(); //當然我也可以這樣寫 sw.Stop(); string aa = sw.Elapsed.ToString();//批量刪除了17597條資料。耗時1.76秒 return View(); } } }