{"record":{"id":"0187af3fd71b9dad","repo":"clockworklabs/SpacetimeDB","slug":"building-dbconnection-with-a-null-nameoraddress-c","errorCode":null,"errorMessage":"Building DbConnection with a null nameOrAddress. Call WithDatabaseName() first.","messagePattern":"Building DbConnection with a null nameOrAddress\\. Call WithDatabaseName\\(\\) first\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sdks/csharp/src/SpacetimeDBClient.cs","lineNumber":37,"sourceCode":"    {\n        readonly DbConnection conn = new();\n\n        string? uri;\n        string? nameOrAddress;\n        string? token;\n        Compression? compression;\n        bool light;\n        bool? confirmedReads;\n\n        public DbConnection Build()\n        {\n            if (uri == null)\n            {\n                throw new InvalidOperationException(\"Building DbConnection with a null uri. Call WithUri() first.\");\n            }\n            if (nameOrAddress == null)\n            {\n                throw new InvalidOperationException(\"Building DbConnection with a null nameOrAddress. Call WithDatabaseName() first.\");\n            }\n            conn.Connect(token, uri, nameOrAddress, compression ?? Compression.Brotli, light, confirmedReads);\n#if UNITY_5_3_OR_NEWER\n            if (SpacetimeDBNetworkManager._instance != null)\n            {\n                SpacetimeDBNetworkManager._instance.AddConnection(conn);\n            }\n#endif\n            return conn;\n        }\n\n        public DbConnectionBuilder<DbConnection> WithUri(string uri)\n        {\n            this.uri = uri;\n            return this;\n        }\n\n        public DbConnectionBuilder<DbConnection> WithDatabaseName(string nameOrAddress)","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/sdks/csharp/src/SpacetimeDBClient.cs#L19-L55","documentation":"The second precondition in DbConnectionBuilder.Build(): the database name or address must be supplied via WithDatabaseName before connecting. Build throws InvalidOperationException when nameOrAddress is still null, after the uri check has passed.","triggerScenarios":"Calling builder.WithUri(...).Build() without WithDatabaseName(...), or the database-name branch being skipped when the value comes from config/env that is unset.","commonSituations":"Database name moved to an env var that is missing in a deploy environment; typos between WithDatabaseName and a similarly named setter; per-tenant connection loops where one tenant row has a null name.","solutions":["Call WithDatabaseName(\"mydb\") (database name or published address) before Build()","Validate required config values up front and fail with your own error naming the missing key","Centralize builder construction so both WithUri and WithDatabaseName are always supplied together"],"exampleFix":"// before\nvar conn = builder\n    .WithUri(\"https://maincloud.spacetimedb.com\")\n    .Build(); // no WithDatabaseName -> throws\n\n// after\nvar conn = builder\n    .WithUri(\"https://maincloud.spacetimedb.com\")\n    .WithDatabaseName(dbName ?? \"mydb\")\n    .Build();","handlingStrategy":"validation","validationCode":"var dbName = config[\"SpacetimeDB:Database\"];\nif (string.IsNullOrWhiteSpace(dbName))\n    throw new ConfigException(\"SpacetimeDB:Database is required before building a connection\");","typeGuard":null,"tryCatchPattern":"try { var conn = builder.Build(); }\ncatch (InvalidOperationException e) when (e.Message.Contains(\"null nameOrAddress\"))\n{ /* resolve the database name and rebuild */ }","preventionTips":["Centralize builder construction so both required setters always run","Validate required config keys at application start, not at connect time"],"tags":["builder","connection","configuration","csharp"],"backgroundTag":"incomplete-builder-configuration","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}