Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Exception "The underlying provider failed on Open"

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?

like image 811
Rajesh Puniya Avatar asked Feb 27 '14 05:02

Rajesh Puniya


People also ask

How do I fix underlying provider failed on Open?

A variation of this issue is when the user that opens the user instance connection has read permissions on the database files but does not have write permissions. If you get a message saying that the database is opened as read only, you need to change the permissions on the database file.

Could not connect to server a network related or instance specific error occurred?

This error usually means that the client can't find the SQL Server instance. This issue occurs when at least one of the following problems exists: The name of the computer hosting SQL Server is incorrect. The instance doesn't resolve the correct IP.


1 Answers

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.

like image 63
Alberto Avatar answered Oct 20 '22 00:10

Alberto