InvalidOperationException:连接未关闭。连接的当前状态为连接中

时间:2018-07-09 04:59:39

标签: c# sql-server codesmith

我有以下使用Codesmith编写的代码(类似于Entity Framework)。 我在代码后得到下面提到的异常。 而且我不会每次都遇到这个例外。该应用程序是WPF和多线程的。

public Dictionary<string, string> GetDelimiterOfAnalyser()
{
    Dictionary<string, string> delimiters = new Dictionary<string, string>();
    try
    {
        foreach (var delimiter in dataContext.DataSource.Where(a => a.TypeId == 1).ToList())
        {
            if (!delimiters.ContainsKey(delimiter.Name))
            {
                delimiters.Add(delimiter.Name, delimiter.Delimiter.Value);
            }
        }
    }
    catch (Exception ex)
    {
        logger.Error("Error in GetDelimiterOfAnalyser. Ex: " + ex);
    }
    return delimiters;
}

我收到此错误:

2018-07-08 11:41:32.4889 ERROR : Error in GetDelimiterOfAnalyser. Ex: System.InvalidOperationException: The connection was not closed. The connection's current state is connecting.
   at System.Data.ProviderBase.DbConnectionClosedConnecting.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
   at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
   at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
   at System.Data.Linq.SqlClient.SqlProvider.CompiledQuery.Execute(IProvider provider, Object[] arguments)
   at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.ExecuteKeyQuery(Object[] keyValues)
   at System.Data.Linq.CommonDataServices.DeferredSourceFactory`1.Execute(Object instance)
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at System.Data.Linq.EntityRef`1.get_Entity()
   at ReconConfig.Data.DataSource.get_Delimiter() in C:\Users\*\OHS.Reconn.DataAccessLayer\Entities\DataSource.Generated.cs:line 407

0 个答案:

没有答案