{"record":{"id":"951b33f57c09fe05","repo":"DapperLib/Dapper","slug":"connection","errorCode":null,"errorMessage":"connection","messagePattern":"connection","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Dapper.ProviderTools/BulkCopy.cs","lineNumber":41,"sourceCode":"            if (connection is null) return null;\r\n            var type = connection.GetType();\r\n            if (!s_bcpFactory.TryGetValue(type, out var func))\r\n            {\r\n                s_bcpFactory[type] = func = CreateBcpFactory(type);\r\n            }\r\n            var obj = func?.Invoke(connection);\r\n            return DynamicBulkCopy.Create(obj);\r\n        }\r\n\r\n        /// <summary>\r\n        /// Create a BulkCopy instance for the connection provided\r\n        /// </summary>\r\n        public static BulkCopy Create(DbConnection connection)\r\n        {\r\n            var bcp = TryCreate(connection);\r\n            if (bcp is null)\r\n            {\r\n                if (connection is null) throw new ArgumentNullException(nameof(connection));\r\n                throw new NotSupportedException(\"Unable to create BulkCopy for \" + connection.GetType().FullName);\r\n            }\r\n            return bcp;\r\n        }\r\n\r\n        ///// <summary>\r\n        ///// Provide an external registration for a given connection type\r\n        ///// </summary>\r\n        //public static void Register(Type type, Func<DbConnection, BulkCopy> factory)\r\n        //{\r\n        //    throw new NotImplementedException();\r\n        //}\r\n\r\n        private static readonly ConcurrentDictionary<Type, Func<DbConnection, object>?> s_bcpFactory\r\n            = new ConcurrentDictionary<Type, Func<DbConnection, object>?>();\r\n\r\n        internal static Func<DbConnection, object>? CreateBcpFactory(Type connectionType)\r\n        {\r","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/DapperLib/Dapper/blob/72a54c475f75e18cb93cba0809d00a5e6e49efd9/Dapper.ProviderTools/BulkCopy.cs#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar bcp = BulkCopy.Create(maybeNullConn);\n\n// after\nif (maybeNullConn is null) throw new InvalidOperationException(\"connection not initialized\");\nvar bcp = BulkCopy.Create(maybeNullConn);\n// or non-throwing:\nvar bcp = BulkCopy.TryCreate(maybeNullConn);","handlingStrategy":"validation","validationCode":"if (connection is null) throw new InvalidOperationException(\"connection is not initialized\");\nvar bcp = BulkCopy.Create(connection);\n// or use the non-throwing factory:\nvar bcp = BulkCopy.TryCreate(connection);\nif (bcp is null) { /* handle unsupported/null */ }","typeGuard":"static DbConnection RequireConnection(DbConnection? c) => c ?? throw new InvalidOperationException(\"connection is null\");","tryCatchPattern":null,"preventionTips":["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."],"tags":["argument-null","bulk-copy","dapper-providertools"],"backgroundTag":null,"analyzedSha":"72a54c475f75e18cb93cba0809d00a5e6e49efd9","analyzedAt":"2026-08-13T14:18:18.115Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}