Verify connection to database in Entity Framework Core

Asked 1 year 11 months 13 days 1 hour 30 minutes ago, Viewed 1768 times

You need to run a query against the database and then examine the results. We don't care about the return value because the query will throw an exception if the execution fails.

VisitorsController.cs
public bool CheckConnection()
{
   bool con = false;
   try
   {
      var books = context.Visitors.FromSqlRaw("SELECT * FROM Visitors LIMIT 1").ToList();
      con = true;
   }
   catch (Exception e)
   {
      con = false;
   }
   return con;
}