EF用原生的插入数据方法DbSet.ADD()和 DbSet.AddRange()都很慢。所以要做大型的批量插入只能另选它法。
1.Nugget
2.代码
using EF6._0Test.EF; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace EF6._0Test { class Program { static void Main(string[] args) { using (Jason_TestEntities db = new Jason_TestEntities()) { List<wolf_example> list = new List<wolf_example>(); for (int i = 0; i < 50000; i++) { list.Add(new wolf_example() { type = i, Name = "Name" + i.ToString(), Money = i, CTime = DateTime.Now, UserID = i, Remak = "Remak" + i.ToString(), Sex = i, IsShow = true, }); } Stopwatch watch = Stopwatch.StartNew(); db.BulkInsert(list); db.BulkSaveChanges(); watch.Stop(); Console.WriteLine(string.Format("{0} 条数据, 用时 {1} milliseconds.", list.Count(), watch.ElapsedMilliseconds)); Console.ReadLine(); } } } }
3.运行效果
时间: 2024-11-05 17:29:06