Using both Entity Framework and SqlConnection?


I am working on a project that contains both legacy code and Entity Framework code. I was advised to use a specific data processing method to work with a record from a database. The entry was received through the Entity Framework and I passed the PK to the dataservice function to perform the operation.

Prerequisites for success and failure

If there was a network , both DB calls (entity and SQL) will succeed. If the net went down and then came back and this code was executed then it will retrieve the locked records using the entity framework and then with SqlException


This got me thinking what is going on here that might crash SqlConnection

even though EF might be able to establish a connection.

Sample code

Below is a sample code:

public void HandleNetworkBecomesAvailable() {



    public void ReleaseRecords()
        using (var context = new Entities()) // using EntityConnection
            var records = context.Records.Where(
                record => record.IsLocked).ToList();

            foreach (var record in records)



    public void UnlockRecord(int recordId)
        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Sqlconnection"].ConnectionString))

            using (var command = connection.CreateCommand())
                command.CommandText = @"UPDATE [Records] SET [IsLocked] = 0";




<add name="EntityConnection" connectionString="metadata=res://*/FooDatabase.csdl|res://*/FooDatabase.ssdl|res://*/FooDatabase.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=RemoteServer;initial catalog=FooDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
<add name="SqlConnection" connectionString="Server=RemoteServer;Database=FooDatabase;Trusted_Connection=True" />


Now, having discussed with my colleagues, I moved the logic into the entity data infrastructure and did the work there. But I'm still not entirely sure why the connection didn't work.

Edit: actual error:

An exception of type "System.Data.SqlClient.SqlException" occurred in System.Data.dll but was not handled in user code

Additional Information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not available. Check if the instance name is correct and configure SQL Server for remote connection. (Provider: Named Pipes Provider, Error: 40 - Failed to open connection to SQL Server)

Inner Exception:

Network path not found

But Entity Framework uses the same network path as the two connection strings in App.config.


source to share

1 answer

Could you benefit from a new resiliency framework where it is transparent? If the error is intermittent, you won't even know it was repeated, while reports that it fails as soon as it fails. Just check ...



All Articles