抛出包括连接字符串的错误

时间:2017-07-14 15:57:19

标签: c# sql-server entity-framework error-handling

我有一个使用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中)。有没有尝试... catch ...抛出一段代码,其中包含抛出的异常中的实际连接字符串?

对不起,如果这是一个愚蠢的问题。此外 - 欢迎使用替代建议来访问连接字符串或修复我的基本问题。

1 个答案:

答案 0 :(得分:0)

最终我找到了答案!

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