I lost access to the MS account that owns the NuGet package for this project. A replacement project is planned. - May 31, 2024
Easy and automatic mapping for SqlBulkCopy
- Option 1: Install via package manager
- Option 2: Install via NuGet
Install-Package EasyBulkCopy
- Option 3: Install via dotnet command line interface
dotnet add package EasyBulkCopy
Create classes that map to your MSSQL tables and decorate them with the attribute to provide the table name
[BulkTableName("dbo.MyTable")]
public class MyTable
{
public int Id { get; set; }
public string Name { get; set; }
public DateTimeOffset CreatedDate { get; set; }
}
- Option 1: Piecemeal registration
public void ConfigureServices(IServiceCollection services)
{
services.UseEasyBulkCopy();
services.RegisterBulkInsertType<MyTable>();
}
- Option 2: Scan assembly and automatically register decorated types
public void ConfigureServices(IServiceCollection services)
{
services.UseEasyBulkCopy();
services.RegisterBulkInsertTypesInAssembly(Assembly.GetExecutingAssembly());
}
public class Foo
{
// ...
public Foo(IBulkInserter bulkInserter)
{
_bulkInserter = bulkInserter
_connectionString = "Server=localhost;Database=master;"
}
public async Task Write(IEnumerable<MyTable> rows)
{
await _bulkInserter.Insert(_connectionString, rows);
}
}