引发包含连接字符串的错误



我有一个使用 EF 连接到 SQL Server 数据库的 c# .NET Web 应用程序,它抛出"初始化字符串的格式不符合从索引 0 开始的规范"错误。这只发生在我的远程服务器上,它在本地工作正常。显然,我的连接字符串不正确,我的问题并不直接与此错误有关。

我的应用程序有 5 个上下文与 5 个 SQL Server 数据库匹配,每个上下文在我的 web.config 文件中都有一个连接字符串。我知道第一个上下文正在起作用,但我不知道其他上下文中的哪一个是问题所在。web.config 中的 5 个连接字符串都非常相似,如果第一个(显然有效(正常工作,我不明白为什么任何一个不起作用。所有 5 个连接字符串都曾经工作,发生了什么变化。

所以我想要更多的线索。

这是我的 YSOD 堆栈跟踪:

[ArgumentException: Format of the initialization string does not conform to specification starting at index 0.]
System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) +1739
System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +191
System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +136
System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +76
System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +35
System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +241
System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +78
System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +116
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +931
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +509
System.Data.Entity.Internal.LazyInternalConnection.Initialize() +77
System.Data.Entity.Internal.LazyInternalConnection.get_Connection() +16
System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver) +508
System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) +370
System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +140
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +483
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +177
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +274
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +77
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues) +32
Argus.RdAP.GetCodeFromIndex(Int32 IndexNumber) +140
(etc...)

好的,这是我的问题:如果我能看到错误的连接字符串是什么,那将非常方便。我很清楚问题在代码中的哪个位置开始(即在Argus.RdAP.GetCodeFromIndex中(。有没有尝试...抓住。。。抛出一段代码,该代码将在引发的异常中包含实际的连接字符串?

对不起,如果这是一个愚蠢的问题。另外 - 欢迎访问连接字符串或修复我的潜在问题的替代建议。

最终我找到了答案!

try
{
// here was the method I suspected of causing the problem
}
catch
{
throw new Exception(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
}

相关内容

最新更新