Debaser 0.29.0
dotnet add package Debaser --version 0.29.0
NuGet\Install-Package Debaser -Version 0.29.0
<PackageReference Include="Debaser" Version="0.29.0" />
paket add Debaser --version 0.29.0
#r "nuget: Debaser, 0.29.0"
// Install Debaser as a Cake Addin #addin nuget:?package=Debaser&version=0.29.0 // Install Debaser as a Cake Tool #tool nuget:?package=Debaser&version=0.29.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. net9.0 is compatible. |
.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)
-
net9.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.29.0 | 148 | 11/22/2024 |
0.28.0 | 680 | 9/6/2024 |
0.27.0 | 1,042 | 12/3/2023 |
0.26.0 | 181 | 11/16/2023 |
0.25.0 | 111 | 11/16/2023 |
0.24.0 | 5,190 | 11/9/2022 |
0.23.0 | 4,236 | 11/22/2021 |
0.22.0 | 461 | 9/9/2021 |
0.21.0 | 488 | 8/31/2021 |
0.20.0 | 1,232 | 1/19/2021 |
0.19.0 | 527 | 1/9/2021 |
0.18.0 | 555 | 11/13/2020 |
0.17.0 | 486 | 11/13/2020 |
0.16.0 | 522 | 11/13/2020 |
0.15.0 | 4,482 | 4/2/2020 |
0.14.0 | 682 | 3/20/2020 |
0.13.0 | 553 | 3/18/2020 |
0.12.0 | 561 | 3/18/2020 |
0.11.0 | 550 | 3/17/2020 |
0.10.0 | 538 | 3/17/2020 |
0.9.0 | 1,169 | 2/24/2017 |
0.7.0 | 1,036 | 2/7/2017 |
0.6.0 | 1,040 | 2/7/2017 |
0.5.0 | 1,035 | 2/7/2017 |
0.3.0 | 1,046 | 1/17/2017 |
0.2.0 | 1,018 | 1/13/2017 |
0.1.0 | 1,033 | 1/13/2017 |
0.0.1 | 1,022 | 11/23/2016 |