zoukankan      html  css  js  c++  java
  • Z.EntityFramework.Extensions.EFCore(实体框架的神奇扩充批量更新批量保存批量删除)特别高效

    // @nuget: Microsoft.EntityFrameworkCore.SqlServer
    // @nuget: Z.EntityFramework.Extensions.EFCore
    // Website: https://entityframework-extensions.net/
    
    using Microsoft.EntityFrameworkCore;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Data.SqlClient;
    using System.Diagnostics;
    
    public class Program
    {
        public static List<BenchmarkResult> BenchmarkResults = new List<BenchmarkResult>();
        
        public static void Main()
        {
            using (var context = new EntityContext())
            {
                context.Database.EnsureCreated();
            }
            
            JustInTime_Compile();
            
            // Generate X entities
            var customers = GenerateCustomers(1000);
            
            var clockSaveChanges = new Stopwatch();
            var clockBulkSaveChanges = new Stopwatch();
            
            using (var context = new EntityContext())
            {
                // SaveChanges
                context.Customers.AddRange(customers);
                clockSaveChanges.Start();
                context.SaveChanges();
                clockSaveChanges.Stop();
    
                BenchmarkResults.Add(new BenchmarkResult() { Action = "SaveChanges (Entity Framework)", Entities = customers.Count, Performance = clockSaveChanges.ElapsedMilliseconds + " ms" });
            }
            using (var context = new EntityContext())
            {
                // BulkSaveChanges
                context.Customers.AddRange(customers);
                clockBulkSaveChanges.Start();
                context.BulkSaveChanges(); // performance can be improved with options
                clockBulkSaveChanges.Stop();
    
                BenchmarkResults.Add(new BenchmarkResult() { Action = "BulkSaveChanges", Entities = customers.Count, Performance = clockBulkSaveChanges.ElapsedMilliseconds + " ms" });
            }
            
            FiddleHelper.WriteTable("EFE - High-performance bulk operations", BenchmarkResults);
        }
             
        public static void JustInTime_Compile()
        {
            var customers = GenerateCustomers(20);
            
            // SaveChanges
            using (var context = new EntityContext())
            {
                context.Customers.AddRange(customers);
                context.SaveChanges();
                context.Customers.RemoveRange(customers);
                context.SaveChanges();
            }
            
            // BulkSaveChanges
            using (var context = new EntityContext())
            {
                context.Customers.AddRange(customers);
                context.BulkSaveChanges();
                context.Customers.RemoveRange(customers);
                context.BulkSaveChanges();
            }
        }
        
        public static List<Customer> GenerateCustomers(int count)
        {
            var list = new List<Customer>();
            
            for(int i = 0; i < count; i++)
            {
                list.Add(new Customer() { Name = "Customer_" + i, Description = "Description_" +  i, IsActive = i % 2 == 0 });
            }
    
            return list;
        }
    
        public class EntityContext : DbContext
        {
            public EntityContext() 
            {
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
    
                base.OnConfiguring(optionsBuilder);
            }
            
            public DbSet<Customer> Customers { get; set; }
        }
    
        public class Customer
        {
            public int CustomerID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public Boolean IsActive { get; set; }
        }
        
        public class BenchmarkResult
        {
            public string Action { get; set; }
            public int Entities { get; set; }
            public string Performance { get; set; }
        }
    }
  • 相关阅读:
    Python基础练习
    AngularJS学习之旅—AngularJS 过滤器(七)
    AngularJS学习之旅—AngularJS 控制器(六)
    AngularJS学习之旅—AngularJS Scope作用域(五)
    AngularJS学习之旅—AngularJS 模型(四)
    AngularJS学习之旅—AngularJS 指令(三)
    AngularJS学习之旅—AngularJS 表达式(二)
    AngularJS学习之旅—AngularJS 简介(一)
    SQL Server排名或排序的函数
    Asp.net导出Excel/Csv文本格式数据
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/14206836.html
Copyright © 2011-2022 走看看