12

In normal entity framework I am able to check the database connectivity using dbContext.Database.Exists() but in Entity Framework Core it is not there. What is the alternative of dbContext.Database.Exists() in Entity Framework Core?

3
  • 1
    Do you mean check connection status ? Commented Sep 18, 2017 at 17:57
  • 1
    I just want to check whether my connection string is valid and It can successfully connect to database. Commented Sep 18, 2017 at 18:04
  • 1
    You can check Status property in connection instance Commented Sep 18, 2017 at 19:03

3 Answers 3

28

You can determine whether or not the database is available and can be connected to with the CanConnect() method:

if (dbContext.Database.CanConnect())
{
    // all good
}

You can use CanConnectAsync() for asynchronous operation.

Sign up to request clarification or add additional context in comments.

7 Comments

CanConnect() applies to Entity Framework Core 2.2 and 3.0 at the time of this comment. Source
If your server is offline, CanConnect() throws an exception, it doesn't always return true/false. Did you guys notice this?
@Tony, there was a recent issue on Github about this recently. It should handle exceptions itself and return false instead in the case of a connection failure.
@nmur I see it, thank you for the reference. It seems like it will be available as of 5.0 and on. We're using 3.1.x and that fix did not make it in time.
Is there a way to see if I can connect to the server (even the DB doesn't exist), for example, would this connection allow me to create the DB should I choose to?
|
6

Currently (up to the latest at this time EF Core 2.0) the DatabaseFacade class (which is the type of the DbContext.Database property) does not expose publicly Exists method.

However, the equivalent of the corresponding EF6 method is provided by the EF Core IRelationalDatabaseCreator service. You can expose it with a custom extension method like this:

using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;

public static class DatabaseFacadeExtensions
{
    public static bool Exists(this DatabaseFacade source)
    {
        return source.GetService<IRelationalDatabaseCreator>().Exists();
    }
}

But please note that the Exists method was never intended to check the database connectivity, but rather than check if the database needs to be created (used internally when you call methods like EnsureCreated, Migrate etc.).

3 Comments

what do you think is a good method for testing database health? I'm trying to implement one in my load balancer health check.
How about a: await _context.Database.ExecuteSqlCommandAsync("SELECT 1");
@ArcadeRenegade I can't say exactly. I would create (yet another) custom (extension) method and play with the implementation - I envision it more like OpenConnection / CloseConnection call pair.
3

The "Exists()" method only check whether the database exists or not, It doesn't really check whether your application can connect to the database or not. For example: if the password is wrong in connection string even then Exists() method will return true. So according to me, a better solution would be open the connection and check if any exception occurs in that.

        try
        {
             dbContext.Database.OpenConnection();
             dbContext.Database.CloseConnection();
             return true;
        }
        catch (Exception ex)
        {
            return false;
        }

but if you still want to use Exists() then you can use in this way

dbContext.Database.GetService<IRelationalDatabaseCreator>().Exists();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.