Verify connection to database in Entity Framework Core

Asked 1 year 10 months 26 days 13 hours 8 minutes ago, Viewed 1756 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;
}