DapperLib/Dapper · error · ArgumentNullException
connection
Error message
connection
What it means
Thrown by BulkCopy.Create(DbConnection) when the supplied connection is null. TryCreate(null) returns null (it guards null at its entry), so Create falls into its null-result branch and re-checks connection, throwing ArgumentNullException with paramName "connection". This is the null-argument guard distinct from the NotSupportedException that fires for unsupported provider types on the very next line.
Source
Thrown at Dapper.ProviderTools/BulkCopy.cs:41
if (connection is null) return null;
var type = connection.GetType();
if (!s_bcpFactory.TryGetValue(type, out var func))
{
s_bcpFactory[type] = func = CreateBcpFactory(type);
}
var obj = func?.Invoke(connection);
return DynamicBulkCopy.Create(obj);
}
/// <summary>
/// Create a BulkCopy instance for the connection provided
/// </summary>
public static BulkCopy Create(DbConnection connection)
{
var bcp = TryCreate(connection);
if (bcp is null)
{
if (connection is null) throw new ArgumentNullException(nameof(connection));
throw new NotSupportedException("Unable to create BulkCopy for " + connection.GetType().FullName);
}
return bcp;
}
///// <summary>
///// Provide an external registration for a given connection type
///// </summary>
//public static void Register(Type type, Func<DbConnection, BulkCopy> factory)
//{
// throw new NotImplementedException();
//}
private static readonly ConcurrentDictionary<Type, Func<DbConnection, object>?> s_bcpFactory
= new ConcurrentDictionary<Type, Func<DbConnection, object>?>();
internal static Func<DbConnection, object>? CreateBcpFactory(Type connectionType)
{
View on GitHub (pinned to 72a54c475f)
Solutions
- Null-check the connection before calling BulkCopy.Create, or prefer the non-throwing BulkCopy.TryCreate(connection) which returns null instead of throwing.
- Ensure the connection is constructed from a concrete provider type (e.g. SqlConnection) so that when non-null, the convention-based factory can locate the matching BulkCopy class.
Example fix
// before
var bcp = BulkCopy.Create(maybeNullConn);
// after
if (maybeNullConn is null) throw new InvalidOperationException("connection not initialized");
var bcp = BulkCopy.Create(maybeNullConn);
// or non-throwing:
var bcp = BulkCopy.TryCreate(maybeNullConn); Defensive patterns
Strategy: validation
Validate before calling
if (connection is null) throw new InvalidOperationException("connection is not initialized");
var bcp = BulkCopy.Create(connection);
// or use the non-throwing factory:
var bcp = BulkCopy.TryCreate(connection);
if (bcp is null) { /* handle unsupported/null */ } Type guard
static DbConnection RequireConnection(DbConnection? c) => c ?? throw new InvalidOperationException("connection is null"); Prevention
- Prefer BulkCopy.TryCreate for nullable inputs; reserve Create for values you have already validated.
- Centralize connection construction so null references surface at the DI/factory boundary, not inside Dapper calls.
When it happens
Trigger: Calling BulkCopy.Create(null) directly; passing a connection variable that resolved to null (e.g. a factory returned null, or a connection field was never assigned) into BulkCopy.Create.
Common situations: Dependency-injection wiring that forgot to register a DbConnection; a connection built lazily whose initializer failed silently; code that reuses a disposed/null connection reference.
Related errors
AI-assisted analysis of DapperLib/Dapper@72a54c475f (2026-08-13).
Data as JSON: /api/errors/951b33f57c09fe05.
Report an issue: GitHub.