Check to determine whether Entity Framework is associated with anything.

.net entity-framework

Question

The connection does not attempt to access the database when a new EntityCollection object is created; it waits until you attempt to perform some action on the collection. I need to know if an Entity collection has a valid connection or not, but I can't seem to find an effective way to achieve it.

I currently have the following in my code:

var db = new MyEntityCollection();

try
{
     var checkworking = from c in db.Customers select c;
}
catch 
{ 
     ConnectToBackUp();
}

This, in addition to being terrible code, is also very sluggish since it takes a very long time to check if the connection is active before raising an error.

Although I am aware that I can use ConnectionTimeout to adjust how long it waits before giving up, it is simply another unsightly hack that exacerbates an already problematic scenario.

There must be a more effective method to go about this.

1
27
2/22/2011 3:46:36 PM

Accepted Answer

This was resolved by making a fresh connection string to test with ADO after moving around a few homes. Although it is much quicker, employing a try catch is still necessary:

    private bool TestConnection()
    {
        EntityConnectionStringBuilder b = new EntityConnectionStringBuilder();
        ConnectionStringSettings entityConString = ConfigurationManager.ConnectionStrings["MyEntityConnectionString"];
        b.ConnectionString = entityConString.ConnectionString;
        string providerConnectionString = b.ProviderConnectionString;

        SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder();
        conStringBuilder.ConnectionString = providerConnectionString;
        conStringBuilder.ConnectTimeout = 1;
        string constr = conStringBuilder.ConnectionString;

        using (SqlConnection conn = new SqlConnection(constr))
        {
            try
            {
                conn.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
8
2/23/2011 11:49:54 AM

Popular Answer

Simplest:

private bool TestConnection()
{
    var db = new MyEntityCollection();
    int oldTimeOut = db.CommandTimeout;

    try
    {
       db.CommandTimeout = 1;
       db.Connection.Open();   // check the database connection
       return true;
    }
    catch 
    { 
       return false;
    }
    finally 
    {
       db.CommandTimeout = oldTimeOut;
    }
}

Revisions for EF6:

using System.Data.Common;
...

public bool TestConnection() {
    using (var db = new MyEntityCollection()) {
        DbConnection conn = db.Database.Connection;
        try {
            conn.Open();   // check the database connection
            return true;
        }
        catch {
            return false;
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow