EF Core批量插入与删除

using EFPlus.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace EFPlus
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //GetCount();
            await MultiDeleteWithSql();

            stopwatch.Stop();  //停止Stopwatch
            Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出,如:时:分:秒

            Console.WriteLine("Hello World!");
        }

        //static void Main(string[] args)
        //{
        //    Stopwatch stopwatch = new Stopwatch();
        //    stopwatch.Start();

        //    MultiDelete();

        //    stopwatch.Stop();  //停止Stopwatch
        //    Console.WriteLine("Using Elapsed output runTime:{0}", stopwatch.Elapsed.ToString());//这里使用时间差来输出,如:时:分:秒

        //    Console.WriteLine("Hello World!");
        //}


        static void GetCount()
        {
            using (var db = new BlogDbContext())
            {
                var allblogs = db.Blogs.Count();
                Console.WriteLine(allblogs);
            }

        }

        static void MultiDelete()
        {
            using (var db = new BlogDbContext())
            {
                var allblogs = db.Blogs.ToArrayAsync();
                db.Blogs.RemoveRange(allblogs.Result);
                db.SaveChanges();
            }
            
        }

        static async Task MultiDeleteAsync()
        {
            using (var db = new BlogDbContext())
            {
                var allblogs = db.Blogs.ToArrayAsync();
                db.Blogs.RemoveRange(allblogs.Result);
                await db.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        static void MultiInsert()
        {
            using (var db = new BlogDbContext())
            {
                for (int i = 0; i < 1000000; i++)
                {
                    var blog = new Blog { Url = "https://www.cnblog.com/p/" + i.ToString() };
                    db.Blogs.Add(blog);
                }
                db.SaveChanges();
            }

        }

        static async Task MultiInsertAsync()
        {
            using (var db = new BlogDbContext())
            {

                for (int i = 0; i < 1000000; i++)
                {
                    var blog = new Blog { Url = "https://www.cnblog.com/p/" + i.ToString() };
                    db.Blogs.Add(blog);
                }
                await db.SaveChangesAsync();
            }
        }

        static async Task MultiDeleteWithSql()
        {
            var db = new BlogDbContext();
            var conn = db.Database.GetDbConnection();
            try
            {
                await conn.OpenAsync();
                using (var command = conn.CreateCommand())
                {
                    string query = "delete from blogs where Id<1000001";
                    command.CommandText = query;
                    var rows = await command.ExecuteNonQueryAsync();
                }
            }
            finally
            {
                conn.Close();
            }
        }

    }
}

在release模式下,localdb批量插入一百万数据,用时: 

 

localdb批量删除一百万,用时(等了3分钟...不等了):

 

异步方法下批量插入一百万数据:用时:

异步方法下批量删除一百万数据:用时:

 

EF执行原生sql删除一百万:

 

在sql server 2008 R2数据库批量插入一百万数据:

 

 在sql server 2008 R2数据库批量删除一百万数据(3分钟不等了...):

 

用EF执行原生sql批量删除:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值