Verify connection to database in Entity Framework Core

Asked 1 year 9 months 8 days 4 hours 31 minutes ago, Viewed 1723 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;
}