3

Background

I'm working on a project which contains both legacy code and Entity Framework code. I was advised to use a specific dataservice method to operate on a record from the database. The record was retrieved via Entity Framework, and I passed the PK into the dataservice function to perform the operation.

Preconditions for success and failure

If the network was up, both DB calls (entity and SQL) would succeed. If the network went down, then came back up and this code was executed, then it would retrieve the locked records with entity framework but then fail with the SqlException below.

This got me thinking, what is going on here that might cause the SqlConnection to fail despite EF being able to make the connection.

Code Samples

A code sample follows:

public void HandleNetworkBecomesAvailable() {
    _entityFrameworkDataService.ReleaseRecords();
}

EntityFrameworkDataService.cs

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

            foreach (var record in records)
            {
                 _sqlConnectionDataService.UnlockRecord(record.ID);
            }
        }
    }

SqlConnectionDataService.cs

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

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

                command.ExecuteNonQuery();
            }
        }
    }

App.config

<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, after discussing with my coworkers, I ended up moving the logic into the entity framework dataservice and doing the work there. But I'm still not quite sure why the connection kept failing.

Edit: The actual error is:

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 accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Inner Exception:

The network path was not found

But Entity Framework is using the same network path, as can be seen in the two connection strings in the App.config.

12
  • 1
    What was the actual message text of the exception? And was there an inner-exception? Commented Sep 23, 2014 at 16:04
  • It seems that the server cannot be reached, are you sure the connection string used is OK? Commented Sep 23, 2014 at 16:12
  • The exception is basically saying it can't connect to the sql server using the connection string you have provided. If you get that intermittently it is indicative of networking problems rather than anything in the code. If you get it consistently then the connection string you are using is wrong. Commented Sep 23, 2014 at 16:13
  • Yelling at people will not help you to get the answer Commented Sep 23, 2014 at 16:16
  • 1
    No need to be upset :-) If I remember correctly, EF uses special connection strings which might not work properly for "regular" data connections. Show us your connection string (mask out sensitive info of course). Commented Sep 23, 2014 at 16:16

1 Answer 1

1

Could you be benefiting from entity framework's new resiliency, where it retries transparently? If the error is intermittent, you won't even know it retried, whereas ADO.net is letting you know it fails as soon as it fails. Just checking...

Sign up to request clarification or add additional context in comments.

7 Comments

I am inclined to believe this as I know Entity Framework does some form of custom connection management. Could you provide any links that might provide some evidence that this is occurring?
My only concern is that the sql query happens only if the entity framework query succeeds, which means the connection would have to be available.
What value do you set for Multiple active result sets?
No, EF only has connection resilience if you explicitly configure it to do so.
@reckface MARS is set to true for EF
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.