Entity Framework - How to check if table exists?

后端 未结 6 2004
自闭症患者
自闭症患者 2020-12-05 18:13

I\'m using the Entity Framework with Code First approach. The base class DbContext has functions to create and delete the database as well as to check for its existence.

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 18:43

    Assumption: SQL Server

    Catching any old exception when querying the DbSet does not mean the table does not exist.

    Querying a DbSet where the table does not exist will throw an EntityCommandExecutionException with an inner exception of type SqlException. That inner exception has an ErrorNumber property.

    Error number 208 reads (source):

    Invalid object name '%.*ls'.

提交回复
热议问题