Entity Framework Core - How to check if database exists?

前端 未结 2 496
清歌不尽
清歌不尽 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() as RelationalDatabaseCreator).Exists()
    

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

    UPDATE:

    Entity Framework Core 2.0:

    (context.Database.GetService() as RelationalDatabaseCreator).Exists()
    

提交回复
热议问题