Check if a table exists within a database using LINQ

后端 未结 2 749
粉色の甜心
粉色の甜心 2021-01-18 19:52

We have a database that has been deployed to various clients. We are currently introducing a optional new feature that, to be used, will require the customers who want the f

相关标签:
2条回答
  • 2021-01-18 20:32

    A much better option would be to store the version differences as configuration. This could be stored in the database itself, a configuration file or even web.config.

    Otherwise you'll end up with messy code like:

    int result = entity.ExecuteStoreQuery<int>(@"
        IF EXISTS (SELECT * FROM sys.tables WHERE name = 'TableName') 
            SELECT 1
        ELSE
            SELECT 0
        ").SingleOrDefault();
    
    0 讨论(0)
  • 2021-01-18 20:37

    The only possible ways are

    • Query table and get exception
    • Use native SQL to query system views and look for that table - in EFv4 you can execute query directly from ObjectContext by calling ExecuteStoreQuery.

    Your entity model will still have this table so in my opinion you should simply ship your DB with that table and in application code handle if feature is allowed or not (table will not be used but will be in DB).

    If you want to make modular system then whole your feature (including application code) should not be present when client don't want to use it.

    0 讨论(0)
提交回复
热议问题