Giter Site home page Giter Site logo

developeramarish / simplebulkoperations Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phongnguyend/entityframeworkcore.sqlserver.simplebulks

0.0 0.0 0.0 14 KB

a very simple .netcore library that supports bulk insert and bulk update database operations

C# 100.00%

simplebulkoperations's Introduction

SimpleBulkOperations

This is a very simple .netcore library that inserts and updates a large number of records into database using the SqlBulkCopy class. ย 

SimpleBulkOperations supports:

  • Bulk insert
  • Bulk update
  • Bulk delete

Overview

This project provide 2 extension methods so that you can use it with your EntityFrameworkCore DbContext class.

License

Free to copy, modify and use for whatever you want in your applications.

Examples

class Program
{
   static void Main(string[] args)
   {
       //InsertUsingEF();
       //UpdateUsingEF();
       InsertUsingBulkInsert();
       UpdateUsingBulkUpdate();
       DeleteUsingBulkDelete();
       Console.WriteLine("Finished!");
       Console.ReadLine();
   }

   private static void InsertUsingEF()
   {
       Stopwatch watch = new Stopwatch();
       watch.Start();
       using (var dbct = new DemoDbContext())
       {
           var rows = new List<Row>();
           for (int i = 0; i < 500000; i++)
           {
               rows.Add(new Row
               {
                   Column1 = i,
                   Column2 = "" + i,
                   Column3 = DateTime.Now
               });
           }

           dbct.Rows.AddRange(rows);
           dbct.SaveChanges();
       }
       watch.Stop();

       var elapsedTime = watch.Elapsed;
       Console.WriteLine(elapsedTime);
   }

   private static void UpdateUsingEF()
   {
       Stopwatch watch = new Stopwatch();
       watch.Start();
       using (var dbct = new DemoDbContext())
       {
           var rows = dbct.Rows.ToList();

           foreach (var row in rows)
           {
               row.Column2 = "abc";
               row.Column3 = DateTime.Now;
           }

           dbct.SaveChanges();
       }
       watch.Stop();

       var elapsedTime = watch.Elapsed;
       Console.WriteLine(elapsedTime);
   }

   private static void InsertUsingBulkInsert()
   {
       Stopwatch watch = new Stopwatch();
       watch.Start();
       using (var dbct = new DemoDbContext())
       {
           var rows = new List<Row>();
           for (int i = 0; i < 500000; i++)
           {
               rows.Add(new Row
               {
                   Column1 = i,
                   Column2 = "" + i,
                   Column3 = DateTime.Now
               });
           }
           //dbct.BulkInsert(rows, "Rows", "Column1", "Column2", "Column3");
           dbct.BulkInsert(rows, "Rows", row => new { row.Column1, row.Column2, row.Column3 });
       }
       watch.Stop();

       var elapsedTime = watch.Elapsed;
       Console.WriteLine(elapsedTime);
   }

   private static void UpdateUsingBulkUpdate()
   {
       Stopwatch watch = new Stopwatch();
       watch.Start();
       using (var dbct = new DemoDbContext())
       {
           var rows = dbct.Rows.AsNoTracking().ToList();

           foreach (var row in rows)
           {
               row.Column2 = "abc";
               row.Column3 = DateTime.Now;
           }

           //dbct.BulkUpdate(rows, "Rows", "Id", "Column3", "Column2");
           dbct.BulkUpdate(rows, "Rows", row => row.Id, row => new { row.Column3, row.Column2 });
       }
       watch.Stop();

       var elapsedTime = watch.Elapsed;
       Console.WriteLine(elapsedTime);
   }

   private static void DeleteUsingBulkDelete()
   {
       Stopwatch watch = new Stopwatch();
       watch.Start();
       using (var dbct = new DemoDbContext())
       {
           var rows = dbct.Rows.AsNoTracking().ToList();
           //dbct.BulkDelete(rows, "Rows", "Id");
           dbct.BulkDelete(rows, "Rows", row => row.Id);
       }
       watch.Stop();

       var elapsedTime = watch.Elapsed;
       Console.WriteLine(elapsedTime);
   }
}

simplebulkoperations's People

Contributors

phongnguyend avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.