{"record":{"id":"f4a2c70d6a81b0c6","repo":"dotnet/orleans","slug":"the-configured-data-source-creates-connections-of","errorCode":null,"errorMessage":"The configured data source creates connections of type '{actualType.FullName}', but invariant '{invariantName}' uses '{expectedType.FullName}'.","messagePattern":"The configured data source creates connections of type '(.+?)', but invariant '(.+?)' uses '(.+?)'\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/AdoNet/Shared/Storage/DbConnectionFactory.cs","lineNumber":140,"sourceCode":"\n            connection.ConnectionString = connectionString;\n            return connection;\n        }\n\n        public static void ValidateDataSource(string invariantName, DbDataSource dataSource)\n        {\n            ArgumentNullException.ThrowIfNull(dataSource);\n\n            var factory = factoryCache.GetOrAdd(invariantName, GetFactory).Factory;\n            using var expectedConnection = factory.CreateConnection()\n                ?? throw new InvalidOperationException($\"Database provider factory: '{invariantName}' did not return a connection object.\");\n            using var actualConnection = dataSource.CreateConnection();\n\n            var expectedType = expectedConnection.GetType();\n            var actualType = actualConnection.GetType();\n            if (!expectedType.IsAssignableFrom(actualType))\n            {\n                throw new ArgumentException(\n                    $\"The configured data source creates connections of type '{actualType.FullName}', but invariant '{invariantName}' uses '{expectedType.FullName}'.\",\n                    nameof(dataSource));\n            }\n        }\n\n        private class CachedFactory\n        {\n            public CachedFactory(DbProviderFactory factory, string factoryName, string factoryDescription, string factoryAssemblyQualifiedNameKey)\n            {\n                Factory = factory;\n                FactoryName = factoryName;\n                FactoryDescription = factoryDescription;\n                FactoryAssemblyQualifiedNameKey = factoryAssemblyQualifiedNameKey;\n            }\n\n            /// <summary>\n            /// The factory to provide vendor specific functionality.\n            /// </summary>","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/dotnet/orleans/blob/fca799fa70ecb6ad975224271703ca43221f58de/src/AdoNet/Shared/Storage/DbConnectionFactory.cs#L122-L158","documentation":"Thrown by DbConnectionFactory.ValidateDataSource when the DbDataSource supplied to the options creates connections whose .NET type is not assignable to the connection type produced by the provider factory registered for the invariant. Orleans requires the data source and the invariant to agree on connection type so all generated SQL and parameter handling remain valid.","triggerScenarios":"Calling DbConnectionFactory.ValidateDataSource(invariantName, dataSource) where invariantName maps to one provider (e.g. 'Npgsql') but dataSource is a DbDataSource from a different provider (e.g. Microsoft.Data.SqlClient.SqlConnectionDataSource). The expectedType.IsAssignableFrom(actualType) check fails and an ArgumentException is thrown with paramName 'dataSource'.","commonSituations":"Mixing providers: invariant set to PostgreSQL but the DbDataSource built from SQL Server's data source builder (or vice versa); copy-pasting a data source from another service that uses a different connector; upgrading one package but not the other.","solutions":["Make the invariant and the DbDataSource come from the same connector (e.g. invariant 'Npgsql' with NpgsqlDataSource).","If you intentionally changed connectors, update both AdoNetStreamOptions.Invariant and the DbDataSource construction to match.","Drop the DataSource and use ConnectionString only if you cannot align the two."],"exampleFix":"// before\noptions.Invariant = AdoNetInvariants.InvariantNameSqlServer;\noptions.DataSource = new NpgsqlDataSourceBuilder(cs).Build(); // mismatch\n\n// after\noptions.Invariant = AdoNetInvariants.InvariantNamePostgreSql;\noptions.DataSource = new NpgsqlDataSourceBuilder(cs).Build();","handlingStrategy":"validation","validationCode":"// Ensure the data source and invariant agree before startup.\nvar probe = dataSource.CreateConnection();\nvar expected = DbConnectionFactory.CreateConnection(invariant, \"stub\").GetType();\nif (!expected.IsAssignableFrom(probe.GetType()))\n    throw new InvalidOperationException($\"DataSource produces {probe.GetType().FullName}, invariant {invariant} expects {expected.FullName}.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Construct the DbDataSource with the same connector library referenced by the invariant.","Centralize provider selection so invariant and data source cannot drift.","Add a startup self-test that opens a connection through the configured data source."],"tags":["adonet","configuration","datasource","type-mismatch","database","orleans"],"backgroundTag":null,"analyzedSha":"fca799fa70ecb6ad975224271703ca43221f58de","analyzedAt":"2026-08-13T19:55:57.938Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}