之前做项目的时候,做出来的系统的性能不太好,在框架中使用了EntityFramework,于是就在网上查资料,研究如何提高EF的性能。
在这分享一篇博客 批量操作提升EntityFramework的性能
里面提供了一个扩展库Entity Framework扩展库,在这里面找到了一些比较好的方法。下面主要介绍其中的一个方法?-批量添加BulkInsert。
这些扩展方法在哪里找?
在VS中新建EF之后,右键解决方案下的引用, 选择管理NuGet程序包,搜索Z.EntityFramework.Extensions并安装。bulkinsert
然后在类里面添加引用之后就可以直接点出来。
批量添加和EF本身自带的添加性能提高了多少?
下面咱们就用实例说话:
构造一个10W个studentinfo实例:
'''定义要添加数据的条数'''
int customerCount = 100000;
'''定义一个实体集合'''
List<studentInfo> customers = new List<studentInfo>();
'''想集合中添加数据'''
for (int i = 0; i < customerCount; i++)
{
studentInfo customer = new studentInfo()
{
name = "2" + i,
= "2" + i,
studentID = "2" + i,
age = "2"
};
customers.Add(customer);
Console.Write(".");
}
用EF自带的添加方法将数据添加到中,为了计算使用时间,加上StopWatch:
'''开始计时'''
Stopwatch watch = Stopwatch.StartNew();
using (EFTestEntities dbcontext = new EFTestEntities())
{
foreach (var entity in customers)
{
dbcontext.studentInfoes.Add(entity);
}
dbcontext.SaveChanges();
}
'''计时结束'''
watch.Stop();
'''输出时间'''
Console.WriteLine(string.Format("{0} customers are created, cost {1} milliseconds.", customerCount, watch.ElapsedMilliseconds));
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-35086-1.html
有没有人更新了指纹进不去