38

For EF6, I can check whether a database exists in the following way:

context.Database.Exists()

How can I do this in EF Core?

4 Answers 4

55

I have found the solution on my own:

(context.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists()

It works for EF 7.0.0-rc1-final version for SqlServer

UPDATE:

Entity Framework Core 2.0:

(context.Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists()
6
  • 10
    Entity Framework Core 2.0: (context.Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator).Exists()
    – Marcel
    Sep 13, 2017 at 16:51
  • 7
    For EF Core 2.0: There is no need for casting, just use context.Database.GetService<IRelationalDatabaseCreator>() instead
    – Jeroen
    Apr 8, 2019 at 8:28
  • 4
    I had wierd problem with EF Core 3.1. I needed to add manually using Microsoft.EntityFrameworkCore.Infrastructure; in my code. Intellisense didnt tells me what i need to do to get GetService() method. Jan 9, 2020 at 7:10
  • It don't correct working in EF 3.1 for me. For example database exists but Exists() method return false Mar 7, 2020 at 8:01
  • 1
    Note that this will also report false if the database is simply offline or inaccessible to the user due to permissions. In other words, this is really not "exists", but instead more like "can I connect to it right now". Apr 28, 2021 at 19:42
45

UPDATE .Net Core 3.1

To check if a database exists and can be contacted:

dbContext.Database.CanConnect()
6
  • 6
    This throw SqlException error if the database does not exist. Aug 18, 2020 at 11:16
  • 3
    @RachaneeSaengkrajai It would throw an exception if it cannot connect with the SQL server. If the database does not exists but the server is accessible, it just returns false.
    – emp
    Oct 22, 2020 at 19:35
  • For me it returns false in both cases. No exceptions are thrown. Mar 14, 2021 at 0:29
  • Frankly I like this answer better than the accepted one simply because it's clear what you're asking. The Exists() method is really no better than this. Apr 28, 2021 at 19:43
  • This works on EF Core 7 with SQL Server Dec 14, 2022 at 18:13
3

The other solutions tell you whether the database is connectable:

context.Database.GetService<IRelationalDatabaseCreator>().Exists();  //true
context.Database.EnsureDeleted();
context.Database.GetService<IRelationalDatabaseCreator>().Exists();  //true

I want to know whether the database exists:

context.Database.GetService<IRelationalDatabaseCreator>().HasTables();  //true
context.Database.EnsureDeleted();
context.Database.GetService<IRelationalDatabaseCreator>().HasTables();  //false

Note this weird behavior:

context.Database.EnsureDeleted();
context.Database.GetService<IRelationalDatabaseCreator>().HasTables();  //false
context.Database.EnsureCreated();
context.Database.GetService<IRelationalDatabaseCreator>().HasTables();  //true
context.Database.EnsureDeleted();
context.Database.GetService<IRelationalDatabaseCreator>().HasTables();  //true !!

So it's not perfect, but depending on your use case it could be useful.

1
  • 1
    Exists() does return false for me after EnsureDeleted() is called. Feb 17, 2022 at 5:58
0

Entity Framework Core 6.0.7:

dbContext.GetService<IDatabaseCreator>().CanConnect();

works for NpgsqlDatabaseConnector - the Npgsql.EntityFrameworkCore.PostgreSQL 6.0.5 provider and probably for all RelationalDatabaseCreator descendants since it tests if database really exists as the default/base implmentation.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.