0

I have a small situation that rarely occurs but have been tasked with presenting a prettied up check for it.

Currently, if our DB happens to be kicked offline, the system message that gets output is that the standard object has null reference. I could trap this via try/catch block in this one location. However, there can be multiple instances of this.

My main issue is the following: we have a default class we use to call for the DbContext, which is called DatabaseContext.cs. This class inherits DbContext, and in it's constructor has a base call for the connection. See below for how it is called.

public partial class DatabaseContext : DbContext
{
    public DatabaseContext()
        : base(new OracleConnection(Common.Common.Decrypt(ConfigurationManager.
                   ConnectionStrings["Site"].ToString())), true)
{
}

Ignore the Decrypt part (we encrypt our Connection String). My question I have is there a way I can trap that call? I thought about doing a try/catch around it, but wasn't really sure about removing it from the base call at the same time. It probably is that simple, but I want to be doubly sure before I remove it and then have issues with it. Thanks.

IyaTaisho
  • 863
  • 19
  • 42
  • You might want to check this: http://stackoverflow.com/questions/19211082/testing-an-entity-framework-database-connection – Dave Clough Feb 21 '17 at 14:44
  • so what you want to do to check the connectionstring is a valid or DBContext can be created successfully ? – Yashveer Singh Feb 21 '17 at 14:44
  • @YashveerSingh I'm looking at checking to ensure that the DbContext exists and is active. If the connection for whatever reason was severed (DB server down), I need to tell the user as kindly as possible. – IyaTaisho Feb 21 '17 at 14:48
  • @IyaTaisho So the question is you need to check in how much interval ? od it is what you need to test when you do a deployment ? if you need to do it in interval then you can just created a job (Quartz scheduler ) which will do a demo query or just create a DBContext object . – Yashveer Singh Feb 21 '17 at 14:51
  • @YashveerSingh No, I do not need to check the interval. I'm just checking to ensure if I can do the check in the base call whether the connection is active or not. If it is, it's fine and nothing needs to happen. If it is inactive, I need to throw a reason back to the user that they will understand versus a null reference statement. I have a feeling as is I need to extend the call out beyond the base call as is but want to be sure. – IyaTaisho Feb 21 '17 at 14:54
  • so best id when you application start just create a method which try to create a DBContext in try catch if exception happens then you can show msg accordingly . Are you using a Wind app or Web App / – Yashveer Singh Feb 21 '17 at 14:59
  • @YashveerSingh I'm doing an MVC5 web app. I think a better choice might be Application_BeginRequest, because this could happen after the Application_Start has already happened (DB server could crash sometime after the Start). – IyaTaisho Feb 21 '17 at 18:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136277/discussion-between-yashveer-singh-and-iyataisho). – Yashveer Singh Feb 21 '17 at 19:24

1 Answers1

0
     private static bool CheckConnection ()
            {
                bool flag = false;
                try
                {
                    using (var context = new ApplicationDbContext())
                    {


                        if (context.Users.FirstOrDefault() != null)
                        {
                            // thismeans all is well on DBContext
                            flag= true;
                        }
                    }
                }
                catch( Exception t )
                {
                    Debug.WriteLine("Error connecting to DB ");
                    flag= false;
                }
                return flag;
            }


// use it like this in global.assax.cs 


 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

           if( CheckConnection())
            {
                // set session variable of something else to show to user
            }
        }
Yashveer Singh
  • 1,914
  • 2
  • 15
  • 23