FastInsert is a .NET library to bulk insert objects into SQL Server tables.
It uses Source Generators to generate the conversion code during the build instead of using reflection during runtime.
The app needs to target .NET Standard 2.1 or any .NET implementation of it.
Use the .NET CLI to install the package and its dependencies:
dotnet add package FastInsert.SqlServer
The classes targeted for conversion should have the BulkInsert
attribute:
using FastInsert.Core;
namespace MyProject;
[BulkInsert]
public class Customer
{
}
The batch size will be 1000 by default, but it can be customized:
using FastInsert.Core;
namespace MyProject;
[BulkInsert(5000)]
public class Customer
{
}
By default, the class will be mapped to a table with the same name, but this can be overridden using
the System.ComponentModel.DataAnnotations.Schema.TableAttribute
attribute:
[Table("Customers")]
public class Customer
{
}
Also by default, the properties will be mapped to columns with the same name. The name can be overridden using
the System.ComponentModel.DataAnnotations.Schema.ColumnAttribute
attribute:
public class Customer
{
[Column("phone_number")]
public string PhoneNumber { get; set; }
}
To insert the data, you need to inject FastInsert.Core.IDataWriter<T>
:
public class CustomerService(IDataWriter<Customer> dataWriter)
{
public Task SaveCustomers(IEnumerable<Customer> customers, CancellationToken cancellationToken)
{
return dataWriter.WriteAsync(customers, cancellationToken);
}
}
You can configure dependency injection by calling an extension method:
using FastInsert.DependencyInjection;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddFastInsert();
Pull requests are welcome, but please open an issue first to discuss your idea.
Distributed under the GPL-3.0 License. See LICENSE
for more information.
Add support for nested classes
First release