Debaser 0.28.0
dotnet add package Debaser --version 0.28.0
NuGet\Install-Package Debaser -Version 0.28.0
<PackageReference Include="Debaser" Version="0.28.0" />
paket add Debaser --version 0.28.0
#r "nuget: Debaser, 0.28.0"
// Install Debaser as a Cake Addin #addin nuget:?package=Debaser&version=0.28.0 // Install Debaser as a Cake Tool #tool nuget:?package=Debaser&version=0.28.0
Debaser
Have you ever had the need to insert/update many rows in SQL Server? (i.e. "upsert" them)
Did you try to use the MERGE INTO ...
syntax then? (and did you enjoy it?)
Did you know that ADO.NET has an API that allows for STREAMING rows to a temporary table in tempdb
making SQL Server call a stored procedure for each row very quickly?
Did you know that Debaser can do these things for you?
How to do it?
Create a class that looks the way you want it to look:
class SomeDataRow(int id, decimal number, string text)
{
public int Id { get; } = id;
public decimal Number { get; } = number;
public string Text { get; } = text;
}
Debaser supports using a default constructor and properties with setters, or using a constructor with parameters matching the properties like this example shows.
You CAN also use C# records, but Debaser's configuration attributes (like [DebaserKey]
, [DebaserMapper(..)]
, etc.) must be used on properties, so C# classes using the primary constructor syntax as shown above is probably the most compact and neat way to declare your Debaser types.
Then you create the UpsertHelper
for it:
var upsertHelper = new UpsertHelper<SomeDataRow>("db");
(where db
in this case is the name of a connection string in the current app.config)
and then you do this once:
upsertHelper.CreateSchema();
(which will create a table, a table data type, and a stored procedure)
and then you insert 100k rows like this:
var rows = Enumerable.Range(1, 100000)
.Select(i => new SomeDataRow(i, i*2.3m, $"This is row {i}"))
.ToList();
var stopwatch = Stopwatch.StartNew();
await upsertHelper.Upsert(rows);
var elapsedSeconds = stopwatch.Elapsed.TotalSeconds;
Console.WriteLine($"Upserting {rows.Count} rows took {elapsedSeconds} - that's {rows.Count / elapsedSeconds:0.0} rows/s");
which on my machine yields this:
Upserting 100000 rows took 0.753394 - that's 132732.7 rows/s
which I think is fair.
Merging with existing data
By default each row is identified by its ID property. This means that any IDs matching existing rows will lead to an update instead of an insert.
Let's hurl 100k rows into the database again:
var rows = Enumerable.Range(1, 100000)
.Select(i => new SomeDataRow(i, i*2.3m, $"This is row {i}"))
.ToList();
await upsertHelper.Upsert(rows);
and then let's perform some iterations where we pick 10k random rows, mutate them, and upsert them:
var stopwatch = Stopwatch.StartNew();
var updatedRows = rows.InRandomOrder().Take(rowsToChange)
.Select(row => new SomeDataRow(row.Id, row.Number + 1, string.Concat(row.Text, "-HEJ")));
await upsertHelper.Upsert(updatedRows);
which on my machine yields this:
Updating 10000 random rows in dataset of 100000 took average of 0.2 s - that's 57191.1 rows/s
which (again) I think is OK.
Maturity
Debaser is fairly mature. It's been used for some serious things already, but please back your Debaser-based stuff up by some nice integration tests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- fastmember (>= 1.5.0)
- Microsoft.Azure.Services.AppAuthentication (>= 1.6.2)
- microsoft.data.sqlclient (>= 5.2.2)
-
net6.0
- fastmember (>= 1.5.0)
- Microsoft.Azure.Services.AppAuthentication (>= 1.6.2)
- microsoft.data.sqlclient (>= 5.2.2)
-
net7.0
- fastmember (>= 1.5.0)
- Microsoft.Azure.Services.AppAuthentication (>= 1.6.2)
- microsoft.data.sqlclient (>= 5.2.2)
-
net8.0
- fastmember (>= 1.5.0)
- Microsoft.Azure.Services.AppAuthentication (>= 1.6.2)
- microsoft.data.sqlclient (>= 5.2.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.28.0 | 562 | 9/6/2024 |
0.27.0 | 1,038 | 12/3/2023 |
0.26.0 | 177 | 11/16/2023 |
0.25.0 | 108 | 11/16/2023 |
0.24.0 | 5,114 | 11/9/2022 |
0.23.0 | 4,230 | 11/22/2021 |
0.22.0 | 454 | 9/9/2021 |
0.21.0 | 482 | 8/31/2021 |
0.20.0 | 1,225 | 1/19/2021 |
0.19.0 | 521 | 1/9/2021 |
0.18.0 | 547 | 11/13/2020 |
0.17.0 | 480 | 11/13/2020 |
0.16.0 | 515 | 11/13/2020 |
0.15.0 | 4,476 | 4/2/2020 |
0.14.0 | 676 | 3/20/2020 |
0.13.0 | 546 | 3/18/2020 |
0.12.0 | 555 | 3/18/2020 |
0.11.0 | 545 | 3/17/2020 |
0.10.0 | 532 | 3/17/2020 |
0.9.0 | 1,159 | 2/24/2017 |
0.7.0 | 1,027 | 2/7/2017 |
0.6.0 | 1,030 | 2/7/2017 |
0.5.0 | 1,026 | 2/7/2017 |
0.3.0 | 1,036 | 1/17/2017 |
0.2.0 | 1,008 | 1/13/2017 |
0.1.0 | 1,024 | 1/13/2017 |
0.0.1 | 1,013 | 11/23/2016 |