5

I have created a windows service which listen to a TCP/IP port and save received data in database using Entity Framework. Most of the time it works fine but some time its throwing an exception "The underlying provider failed on open." on save data in database. Here is my exception details:

    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)

What is the thing behind this exception and how to resolve it?

Rajesh Puniya
  • 83
  • 1
  • 1
  • 5

5 Answers5

7

Usually, when working with Entity Framework you'll need to enable the multiple active result sets option in your connection string by setting MultipleActiveResultSets to true as follows.

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

Verify that it solves your problem.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
Alberto
  • 15,626
  • 9
  • 43
  • 56
0

Check whether you SQL Server is running or not. Just Open Task Manager and Goto Services, Click on Services button below and check whether SQL Server is running or not.

0

In my case, the issue was that the account which I was using to connect to SQL server was not added to app pool identity. Once I added the account 'domain\username' to app pool, it started working.

Raj Chaurasia
  • 1,976
  • 1
  • 11
  • 10
0

Change integrated security= False ; MultipleActiveResultSets=True in your connection string , it should be work.

B.Nishan
  • 536
  • 4
  • 13
0

I had this error but I solved it by Opening Task Manager, Go to Services and checking if my SQL Server is running or not. Your Sql server should be running.