• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created about 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Very simple .net library that supports bulk insert (retain client populated Ids or return db generated Ids), bulk update, bulk delete and bulk merge operations. Lambda Expression is supported.

EntityFrameworkCore.SqlServer.SimpleBulks

A very simple .net core library that can help to sync a large number of records in-memory into the database using the SqlBulkCopy class. Β 

Overview

This library provides extension methods so that you can use with your EntityFrameworkCore DbContext instance: DbContextExtensions.cs or you can use SqlConnectionExtensions.cs to work directly with a SqlConnection instance.

Nuget

https://www.nuget.org/packages/EntityFrameworkCore.SqlServer.SimpleBulks

EntityFrameworkCore.SqlServer.SimpleBulks supports:

  • Bulk Insert
  • Bulk Update
  • Bulk Delete
  • Bulk Merge

Examples

DbContextExtensions:

Using Lambda Expression:

using EntityFrameworkCore.SqlServer.SimpleBulks.BulkDelete;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkMerge;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkUpdate;

// Insert all columns
dbct.BulkInsert(rows);
dbct.BulkInsert(compositeKeyRows);

// Insert selected columns only
dbct.BulkInsert(rows,
    row => new { row.Column1, row.Column2, row.Column3 });
dbct.BulkInsert(compositeKeyRows,
    row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 });

dbct.BulkUpdate(rows,
    row => new { row.Column3, row.Column2 });
dbct.BulkUpdate(compositeKeyRows,
    row => new { row.Column3, row.Column2 });

dbct.BulkMerge(rows,
    row => row.Id,
    row => new { row.Column1, row.Column2 },
    row => new { row.Column1, row.Column2, row.Column3 },
    options =>
    {
        //options.WithHoldLock = true;
    });
dbct.BulkMerge(compositeKeyRows,
    row => new { row.Id1, row.Id2 },
    row => new { row.Column1, row.Column2, row.Column3 },
    row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 },
    options =>
    {
        //options.WithHoldLock = true;
    });
                        
dbct.BulkDelete(rows);
dbct.BulkDelete(compositeKeyRows);

Using Dynamic String:

using EntityFrameworkCore.SqlServer.SimpleBulks.BulkDelete;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkMerge;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkUpdate;

dbct.BulkUpdate(rows,
    new [] { "Column3", "Column2" });
dbct.BulkUpdate(compositeKeyRows,
    new [] { "Column3", "Column2" });

dbct.BulkMerge(rows,
    "Id",
    new [] { "Column1", "Column2" },
    new [] { "Column1", "Column2", "Column3" },
    options =>
    {
        //options.WithHoldLock = true;
    });
dbct.BulkMerge(compositeKeyRows,
    new [] { "Id1", "Id2" },
    new [] { "Column1", "Column2", "Column3" },
    new [] { "Id1", "Id2", "Column1", "Column2", "Column3" },
    options =>
    {
        //options.WithHoldLock = true;
    });

Using Builder Approach in case you need to mix both Dynamic & Lambda Expression:

new BulkInsertBuilder<Row>(dbct.GetSqlConnection())
	.WithData(rows)
	.WithColumns(row => new { row.Column1, row.Column2, row.Column3 })
	// or .WithColumns(new [] { "Column1", "Column2", "Column3" })
	.WithOuputId(row => row.Id)
	// or .WithOuputId("Id")
	.ToTable(dbct.GetTableName(typeof(Row)))
	// or .ToTable("Rows")
	.Execute();

SqlConnectionExtensions:

Using Lambda Expression:

using EntityFrameworkCore.SqlServer.SimpleBulks.BulkDelete;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkMerge;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkUpdate;

// Register Type - Table Name globaly
TableMapper.Register(typeof(Row), "Rows");
TableMapper.Register(typeof(CompositeKeyRow), "CompositeKeyRows");

connection.BulkInsert(rows,
           row => new { row.Column1, row.Column2, row.Column3 });
connection.BulkInsert(compositeKeyRows,
           row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 });

connection.BulkUpdate(rows,
           row => row.Id,
           row => new { row.Column3, row.Column2 });
connection.BulkUpdate(compositeKeyRows,
           row => new { row.Id1, row.Id2 },
           row => new { row.Column3, row.Column2 });

connection.BulkMerge(rows,
           row => row.Id,
           row => new { row.Column1, row.Column2 },
           row => new { row.Column1, row.Column2, row.Column3 },
           options =>
           {
               //options.WithHoldLock = true;
           });
connection.BulkMerge(compositeKeyRows,
           row => new { row.Id1, row.Id2 },
           row => new { row.Column1, row.Column2, row.Column3 },
           row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 },
           options =>
           {
               //options.WithHoldLock = true;
           });
                        
connection.BulkDelete(rows, row => row.Id);
connection.BulkDelete(compositeKeyRows, row => new { row.Id1, row.Id2 });

Using Dynamic String:

using EntityFrameworkCore.SqlServer.SimpleBulks.BulkDelete;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkMerge;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkUpdate;

connection.BulkInsert(rows, "Rows",
           new [] { "Column1", "Column2", "Column3" });
connection.BulkInsert(rows.Take(1000), "Rows",
           typeof(Row).GetDbColumnNames("Id"));
connection.BulkInsert(compositeKeyRows, "CompositeKeyRows",
           new [] { "Id1", "Id2", "Column1", "Column2", "Column3" });

connection.BulkUpdate(rows, "Rows",
           "Id",
           new [] { "Column3", "Column2" });
connection.BulkUpdate(compositeKeyRows, "CompositeKeyRows",
           new [] { "Id1", "Id2" },
           new [] { "Column3", "Column2" });

connection.BulkMerge(rows, "Rows",
           "Id",
           new [] { "Column1", "Column2" },
           new [] { "Column1", "Column2", "Column3" },
           options =>
           {
               //options.WithHoldLock = true;
           });
connection.BulkMerge(compositeKeyRows, "CompositeKeyRows",
           new [] { "Id1", "Id2" },
           new [] { "Column1", "Column2", "Column3" },
           new [] { "Id1", "Id2", "Column1", "Column2", "Column3" },
           options =>
           {
               //options.WithHoldLock = true;
           });

connection.BulkDelete(rows, "Rows", "Id");
connection.BulkDelete(compositeKeyRows, "CompositeKeyRows", new [] { "Id1", "Id2" });

Using Builder Approach in case you need to mix both Dynamic & Lambda Expression:

new BulkInsertBuilder<Row>(connection)
	.WithData(rows)
	.WithColumns(row => new { row.Column1, row.Column2, row.Column3 })
	// or .WithColumns(new [] { "Column1", "Column2", "Column3" })
	.WithOuputId(row => row.Id)
	// or .WithOuputId("Id")
	.ToTable("Rows")
	.Execute();

License

EntityFrameworkCore.SqlServer.SimpleBulks is licensed under the MIT license.

More Repositories

1

Practical.CleanArchitecture

Full-stack .Net 8 Clean Architecture (Microservices, Modular Monolith, Monolith), Blazor, Angular 16, React 18, Vue 3, BFF with YARP, Domain-Driven Design, CQRS, SOLID, Asp.Net Core Identity Custom Storage, OpenID Connect, Entity Framework Core, Selenium, SignalR, Hosted Services, Health Checks, Rate Limiting, Cloud Services (Azure, AWS, Google)...
C#
1,736
star
2

Practical.NET

πŸš€ C# .NET Developer Roadmap & Asp.Net Core Developer Roadmap in 2023
408
star
3

sql-server-kit

πŸ”¨ SQL Server trouble shooting scripts: query plans, indexes, statistics, deadlocks, I/O, memory, CPU, extended events, wait statistics, ...
TSQL
69
star
4

Practical.DevSecOps

πŸ“š Useful DevSecOps documentation for .NET developers
HCL
57
star
5

DddDotNet

πŸ› οΈ Bunch of Building Blocks for https://github.com/phongnguyend/Practical.CleanArchitecture to avoid duplicated & repeated code
C#
52
star
6

Practical.Cryptography

simple extension methods which make working with cryptography easier
C#
33
star
7

practical-front-end

essential front-end skills for .Net developers
28
star
8

PhongNguyen.CodeAnalysis

Simple Roslyn Analyzers implementation to enforce specific coding standards defined within Organizations
C#
19
star
9

Practical.Jwt

Sample Asp.Net Core Jwt Bearer Authentication
C#
18
star
10

Performance.NET

C#
16
star
11

Practical.Azure

C#
8
star
12

Practical.OpenIdConnect

Simple OAuth 2.0 & OIDC Mock Server to debug how Flows work.
C#
8
star
13

Practical.Topself

C#
5
star
14

Practical.NCalc

C#
5
star
15

CSharpDemos

C#
3
star
16

blog.nashtechglobal.com

Code samples for https://blog.nashtechglobal.com/
C#
3
star
17

Practical.ThreadPool

.Net Runtime has its own Managed Thread Pool instance, the simple code implementation in this repo is just for understanding the concept of how Thread Pool works.
C#
3
star
18

Practical.BenchmarkDotNet

C#
2
star
19

Practical.ReverseProxy

C#
2
star
20

Practical.SOLID

C#
2
star
21

Practical.DistributedTransactions

C#
1
star
22

Practical.NoSQL

1
star
23

SimpleMapper

simple object mapper to map data between 2 identical (or almost identical) shape objects for scenarios like: shallow cloning or data archiving, ...
C#
1
star
24

AzureDevOps.NuGetFeedManager

C#
1
star
25

Practical.Salesforce

C#
1
star
26

Practical.AspNetCore.Identity

C#
1
star
27

EntityFrameworkCore.PostgreSQL.SimpleBulks

Very simple .Net library that supports bulk insert (retain client populated Ids or return db generated Ids), bulk update, bulk delete, bulk merge and bulk match operations. Lambda Expression is supported.
C#
1
star