Verify connection to database in Entity Framework Core

Asked 1 year 10 months 24 days 14 hours 43 minutes ago, Viewed 1755 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;
}