Olibos.Data.SqlClient.SqlBulkCopy
1.0.0
dotnet add package Olibos.Data.SqlClient.SqlBulkCopy --version 1.0.0
NuGet\Install-Package Olibos.Data.SqlClient.SqlBulkCopy -Version 1.0.0
<PackageReference Include="Olibos.Data.SqlClient.SqlBulkCopy" Version="1.0.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Olibos.Data.SqlClient.SqlBulkCopy" Version="1.0.0" />
<PackageReference Include="Olibos.Data.SqlClient.SqlBulkCopy"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Olibos.Data.SqlClient.SqlBulkCopy --version 1.0.0
#r "nuget: Olibos.Data.SqlClient.SqlBulkCopy, 1.0.0"
#:package Olibos.Data.SqlClient.SqlBulkCopy@1.0.0
#addin nuget:?package=Olibos.Data.SqlClient.SqlBulkCopy&version=1.0.0
#tool nuget:?package=Olibos.Data.SqlClient.SqlBulkCopy&version=1.0.0
Olibos.Data.SqlClient.SqlBulkCopy
A high-performance C# source generator that automatically creates SQL bulk copy adapters for your domain models. Eliminates boilerplate code and enables seamless bulk data insertion into SQL Server using IAsyncEnumerable<T>.
Features
β¨ Zero Boilerplate - Automatic code generation for bulk copy operations
β‘ High Performance - Optimized for large-scale data imports
π― Type-Safe - Compile-time validation of bulk copy mappings
π¦ Easy Integration - Simple attribute-based API
π Async/Await Support - Full async support with cancellation tokens
π¨ Clean Code - Works seamlessly with modern C# patterns (IAsyncEnumerable)
Installation
Install via NuGet Package Manager:
dotnet add package Olibos.Data.SqlClient.SqlBulkCopy
Or via Package Manager Console:
Install-Package Olibos.Data.SqlClient.SqlBulkCopy
Quick Start
1. Annotate Your Model
Add the [SqlBulkCopy] attribute to any class you want to bulk copy:
using Olibos.Data.SqlClient.SqlBulkCopy;
[SqlBulkCopy]
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime CreatedAt { get; set; }
}
2. Use the Generated Extensions
Build your project, and a source generator automatically creates optimized bulk copy methods:
using Microsoft.Data.SqlClient;
await using var connection = new SqlConnection("YOUR_CONNECTION_STRING");
await connection.OpenAsync();
using var bulkCopy = new SqlBulkCopy(connection)
{
DestinationTableName = "dbo.Products"
};
await bulkCopy.WriteToServerAsync(GetProductsAsync());
async IAsyncEnumerable<Product> GetProductsAsync()
{
for (int i = 0; i < 10000; i++)
{
yield return new Product
{
Id = i,
Name = $"Product {i}",
Price = 99.99m,
CreatedAt = DateTime.UtcNow
};
}
}
How It Works
This project uses Roslyn source generators to:
- Detect - Finds all classes decorated with
[SqlBulkCopy] - Analyze - Extracts property information from your models
- Generate - Creates optimized
WriteToServerAsyncextension methods - Integrate - Seamlessly integrates into your compilation
Generated Code Example
For a model like:
[SqlBulkCopy]
public class User
{
public int Id { get; set; }
public string Email { get; set; }
}
The generator creates:
public static partial class SqlBulkCopyExtensions
{
public static async Task WriteToServerAsync(
this SqlBulkCopy sql,
IAsyncEnumerable<User> items,
CancellationToken cancellationToken = default)
{
await using var reader = new global_User(items);
await sql.WriteToServerAsync(reader, cancellationToken);
}
private class global_User(
IAsyncEnumerable<User> enumerable,
CancellationToken cancellationToken = default)
: BaseAsyncEnumerableReader
{
// Implementation details...
}
}
Project Structure
Olibos.Data.SqlClient.SqlBulkCopy/
βββ Olibos.Data.SqlClient.SqlBulkCopy/ # Source generator implementation
β βββ SqlBulkCopySourceGenerator.cs # Main generator logic
β βββ Olibos.Data.SqlClient.SqlBulkCopy.csproj
βββ Olibos.Data.SqlClient.SqlBulkCopy.Sample/ # Usage examples
β βββ Program.cs
β βββ Examples.cs
β βββ Model.cs
βββ Olibos.Data.SqlClient.SqlBulkCopy.Tests/ # Unit tests
β βββ SourceGeneratorWithAttributesTests.cs
βββ README.md
Building & Development
Prerequisites
- .NET 10.0 SDK or later
- Visual Studio 2022 / Rider / VS Code
Build
dotnet build
Run Tests
dotnet test
Run Sample
cd Olibos.Data.SqlClient.SqlBulkCopy.Sample
dotnet run
Debugging
The project includes a debug configuration in Properties/launchSettings.json for debugging source generators in Visual Studio.
To debug the generator:
- Open the solution in Visual Studio
- Set breakpoints in
SqlBulkCopySourceGenerator.cs - Launch using the provided debug profile
- The sample project will compile and hit your breakpoints
Technical Details
Supported Property Types
The generator automatically creates column mappings for:
- Primitives:
int,long,string,bool,byte,double,decimal,float - Value Types:
DateTime,Guid,TimeSpan - Nullable types:
int?,string?, etc. - Enums
Requirements
- Properties must have both public getter and setter
- Classes must be decorated with
[SqlBulkCopy] - Classes must be public
- Classes must be in the compilation that references the source generator package
Performance Characteristics
- Code Generation Time: Negligible (happens during build)
- Runtime Overhead: Minimal (generated code is optimized)
- Memory Usage: Efficient streaming with
IAsyncEnumerable - Throughput: Limited primarily by database and network, not the generator
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Learning Resources
To learn more about Roslyn source generators:
- Source Generators Cookbook
- Let's Build an Incremental Source Generator With Roslyn - Stefan PΓΆlz
- Microsoft Source Generators Documentation
FAQ
Q: Do I need to manually map columns?
A: By default, the generator uses positional mapping β properties are mapped to columns in the order they appear. If you need to map by column name or use a different order, you can use the ColumnMappings property of SqlBulkCopy:
using var bulkCopy = new SqlBulkCopy(connection);
bulkCopy.ColumnMappings.Add("sourceColumnName", "destinationColumnName");
await bulkCopy.WriteToServerAsync(items);
This allows you to map properties to different column names or reorder them as needed.
Q: What if I have a large number of properties?
A: The generator efficiently handles any number of properties. Performance is determined by your database, not the generator.
Support
If you encounter issues or have questions:
- Check the FAQ section
- Review the sample project
- Check existing GitHub Issues
- Create a new issue with details about your problem
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
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 |
|---|---|---|
| 1.0.0 | 97 | 1/15/2026 |