实体框架异常 "The underlying provider failed on Open"(底层提供程序在打开操作上失败)

5

我创建了一个 Windows 服务,它监听 TCP/IP 端口并使用 Entity Framework 将接收到的数据保存在数据库中。大部分时间它能正常工作,但有时在保存数据到数据库时会抛出异常 "The underlying provider failed on open."。

以下是我的异常细节:

    Exception: 2/27/2014 10:31 AM:
    The underlying provider failed on Open.
     at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
     at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
     at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.LazyInitValue()
     at System.Lazy`1.get_Value()
     at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
     at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
     at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
     at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
     at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
    at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
    at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
    at Service.DemoService.Save(String received, TcpClient client)

这个异常背后的原因是什么,如何解决?

请检查您的连接字符串。 - Bhupendra Shukla
我的连接字符串 <add name="conn" connectionString="Data Source=.\;Initial Catalog=TDB;UID=admin123;PWD=123;" providerName="System.Data.SqlClient" /> - Rajesh Puniya
6个回答

7
通常情况下,在使用Entity Framework时,您需要通过将MultipleActiveResultSets设置为true来在连接字符串中启用多个活动结果集选项,如下所示。
<add name="conn" 
  connectionString="
    Data Source=.\;
    Initial Catalog=TDB;
    UID=admin123;
    PWD=123;
    MultipleActiveResultSets=True"
  providerName="System.Data.SqlClient" />

请确认它解决了您的问题。


6
我已将MultipleActiveResultSets设置为True,但仍然遇到此错误。 - Raj Chaurasia

0
在我的情况下,问题是我用来连接SQL服务器的帐户没有添加到应用程序池的标识中。一旦我将帐户“域\用户名”添加到应用程序池中,它就开始工作了。

0

检查你的SQL Server是否正在运行。 只需打开任务管理器并转到服务,点击下面的服务按钮,检查SQL Server是否正在运行。


0

我遇到了这个错误,但我通过打开任务管理器、转到服务并检查我的SQL服务器是否正在运行来解决它。你的SQL服务器应该在运行。


1
嗨Gomotso - 我认为你的答案与用户3139697给出的答案相同。 - pettys

0

在你的连接字符串中更改 integrated security= False ; MultipleActiveResultSets=True,它应该可以工作。


0
我正在使用所有这些,但是这仍然出现相同的错误,然后我使用这个,这修复了我的错误。

Max Pool Size=100;

<add name="conn" 
 connectionString="
 Data Source=.\;
 Initial Catalog=TDB;
 UID=admin123;
 PWD=123;
 Max Pool Size=100;
 MultipleActiveResultSets=True"
 providerName="System.Data.SqlClient" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接