Entity Framework - How to check if table exists?

后端 未结 6 2005
自闭症患者
自闭症患者 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 19:04

    An alternative method; it's not as efficient as Ladislav's, but it's not tied to SQL Server (edited to add Where clause to address performance issue):

    bool CheckTableExists()
    {
        try
        {
            context.YourTable.Where(s => s. = ).Count();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
    

提交回复
热议问题