Entity Framework Core - How to check if database exists?

前端 未结 2 482
清歌不尽
清歌不尽 2021-01-01 13:57

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

context.Database.Exists()

How can I do this in EF Core?

相关标签:
2条回答
  • 2021-01-01 14:33

    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()
    
    0 讨论(0)
  • 2021-01-01 14:38

    UPDATE .Net Core 3.1

    To check if a database exists and can be contacted:

    dbContext.Database.CanConnect()
    
    0 讨论(0)
提交回复
热议问题