原文:https://www.cnblogs.com/liuruitao/p/10049191.html

原文:https://www.cnblogs.com/yaopengfei/p/7751545.html

环境:

  就只安装了两个包,除此之外无其它任何配置,
  EntityFramework、Z.EntityFramework.Extensions

 一个简单的测试:

using System;
using System.Collections.Generic;
using System.Data.Entity; namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
using (var db = new BlogEntities())
{
//添加一条数据
//Blog entity = new Blog();
//entity.BlogId = 1;
//entity.Title = "title1";
//entity.CreateDate = DateTime.Now;
//db.Blogs.Add(entity);
//db.SaveChanges(); //查看 添加的数据 和 数据库链接地址
//int i = db.Blogs.ToList().Count;
//var d = db.Blogs.FirstOrDefault();
//var str = db.Database.Connection.ConnectionString; int total = ; //测试1
DateTime dt1 = DateTime.Now;
for (int i = ; i < total; i++)
{
Blog entity = new Blog();
entity.BlogId = ;
entity.Title = "title" + i.ToString();
entity.CreateDate = DateTime.Now;
db.Blogs.Add(entity);
db.SaveChanges();
}
Console.WriteLine("不批量插入1:" + (DateTime.Now - dt1).TotalMilliseconds); //测试2 和测试1的区别 在于 db.SaveChanges(); 的位置
DateTime dt2 = DateTime.Now;
for (int i = ; i < total; i++)
{
Blog entity = new Blog();
entity.BlogId = ;
entity.Title = "title" + i.ToString();
entity.CreateDate = DateTime.Now;
db.Blogs.Add(entity);
}
db.SaveChanges();
Console.WriteLine("不批量插入2:" + (DateTime.Now - dt2).TotalMilliseconds); //测试3 需要引入Z.EntityFramework.Extensions 批量插入
DateTime dt3 = DateTime.Now;
List<Blog> list = new List<Blog>();
for (int i = ; i < total; i++)
{
Blog entity = new Blog();
entity.BlogId = ;
entity.Title = "title" + i.ToString();
entity.CreateDate = DateTime.Now;
list.Add(entity);
}
db.BulkInsert(list);
db.SaveChanges();
Console.WriteLine("批量插入:" + (DateTime.Now - dt3).TotalMilliseconds); Console.ReadKey();
}
}
} public class Blog
{
public int BlogId { get; set; }
public string Title { get; set; }
public DateTime CreateDate { get; set; }
} public class BlogEntities : DbContext
{
public DbSet<Blog> Blogs { get; set; }
} }

EF 批量添加数据的更多相关文章

  1. EF批量添加数据性能慢的问题的解决方案

    //EF批量添加数据性能慢的问题的解决方案 public ActionResult BatchAdd() { using (var db = new ToneRoad.CEA.DbContext.Db ...

  2. EF批量添加数据之修改SQL Server执行上限

    asp.net core 项目 打开Startup.cs services.AddDbContext<MyContext>( options => { options.UseSqlS ...

  3. .Net中批量添加数据的几种实现方法比较

    在.Net中经常会遇到批量添加数据,如将Excel中的数据导入数据库,直接在DataGridView控件中添加数据再保存到数据库等等. 方法一:一条一条循环添加 通常我们的第一反应是采用for或for ...

  4. ThinkPHP批量添加数据和getField()示例

    批量添加数据 // 批量添加数据 $User = M('users'); $dataList[] = array('name'=>'thinkphp','email'=>'thinkphp ...

  5. thinkphp3.2 批量添加数据

    这是我遇到的thinkphp3.2 当中最让我无语的坑 批量添加数据有个方法是 addAll() 这个方法一定要注意数组的键名,一定要整齐!!! 可以在存入数据前,用ksort()方法将数组的键名排序 ...

  6. spring boot之使用通用Mapper批量添加数据

    通用Mapper是一款针对mybatis进行扩展的轻量级组件,使用起来非常方便.当调用其针对mysql进行批量添加数据的方法时,发现报错,仔细研究了一番,发现是在使用通用Mapper上出现了问题.下面 ...

  7. Yii2如何批量添加数据

    批量添加这个操作,在实际开发中经常用得到,今天小编抽空给大家整理些有关yii2批量添加的问题,感兴趣的朋友一起看看吧. 在上篇文章给大家介绍了关于浅析Yii2 gridview实现批量删除教程,当然, ...

  8. ADO.NET- 中批量添加数据的几种实现方法比较

    在.Net中经常会遇到批量添加数据,如将Excel中的数据导入数据库,直接在DataGridView控件中添加数据再保存到数据库等等. 方法一:一条一条循环添加 通常我们的第一反应是采用for或for ...

  9. 使用EF批量新增数据十分缓慢

    使用EF来批量新增数据,发现效率非常的差,几千条数据时甚至需要几分钟来执行,迫于无奈使用sql来执行了. 今天偶然看到一篇关于EF的文章,才发觉原来是自己对EF不够了解的原因. 一般新增时我们是将所有 ...

随机推荐

  1. Springmvc使用注解实现执行定时任务(定时器)

    1.在Spring配置文件中添加 <task:annotation-driven/> 2.在需要调用执行定时任务的类中使用注解 @Service @Lazy(false) //避免spri ...

  2. typora数学符号大全

  3. centerOS7安装lnmp环境

    视频地址: https://www.bilibili.com/video/av55251610?p=65 安装nginx http://nginx.org 点击 download vim /etc/y ...

  4. Docker入门(一):安装

    一. 安装docker 1. 删除已安装的docker yum remove docker \ docker-client \ docker-client-latest \ docker-common ...

  5. 并不对劲的复健训练-p3674

    题目大意 给出序列$ a_1,...,a_n $ ( $ n\leq10^5,a\leq 10^5 $ ),有\(m\) ( \(m\leq 10^5\))个以下三类询问: (1)给出\(l,r,k\ ...

  6. Func<>委托、扩展方法、yield、linq ForEach综合运用

    1.先定义一个Model类    public class P1    {        public string name { get; set; }        public int age ...

  7. z-index神奇的失效了!!!

    z-index简单介绍 首先z-index只对定位元素有效,什么是定位元素呢,也就是设置了position属性的元素,position:relative--相对定位,position:absolute ...

  8. css————关于margin:0px auto的几个居中问题

    前言 margin:0px auto;适用于指定了固定宽度的div与其它元素,比如p,img等,使用 margin:0px auto,居中是大家在做css div定位时的最常用方法,但是据我自己的使用 ...

  9. 原生html、js手写 radio与checkbox 美化

    原生html.js手写 radio与checkbox   美化 html <!DOCTYPE html> <html> <head> <meta charse ...

  10. 7 java 笔记

    1 方法是类或者对象行为特征的抽象,方法是类或对象最重要的组成部分 2 java里面方法的参数传递方式只有一种:值传递 值传递:就是将实际参数值的复制品传入方法内,而参数本身不会受到任何影响.(这是j ...