Entity Framework Core - How to check if database exists?

entity-framework-core

Question

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

context.Database.Exists()

How can I do this in EF Core?

Accepted Answer

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()

Popular Answer

If you are checking whether it exists to determine whether you need to create it, you can instead use this;

context.Database.EnsureCreated();


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why