EF7 - 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 EF7?

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


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



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