Verify connection to database in Entity Framework Core

Asked 1 year 9 months 8 days 1 hour 45 minutes ago, Viewed 1722 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;
}