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批量删除:

 

posted @ 2019-11-04 20:46  星空天宇  阅读(1159)  评论(0编辑  收藏  举报