LINQ may be used to see whether a table exists in a database.

asp.net-mvc c# entity-framework linq

Question

Our database has been made available to a number of customers. We are presently rolling out an optional new feature, however in order to utilize it, customers will need to add a new table to the current database.

We were wondering if it was possible to programmatically determine (with entity framework) whether a table exists in the database because we are releasing new software that will need to interact with versions of the database both with and without the new table (and because we don't want 2 versions, one for customers who have the new table and one for ones who don't) (I can try to access the table and have it throw a exception but was wondering if there was a built in function to do this)

Thanks

Edit: Can someone advise me on how to verify the config file with, for example, a custom data annotation for a mvc controller given that I've been told I should use a config file rather than checking with EF? the following

[Boolean(Properties.Settings.Default.TableExists)]
public class NamedController : Controller

which, if false, produces a page not found error?

Edit 2: I came up with the following solution using the config values suggested by other folks.

A table's existence may be determined by app settings.

<appSettings>
    <add key="tableExists" value="True"/>
</appSettings>

a specific data annotation indicating whether to provide controller access

[AuthoriseIfTableExistsIsTrue]
public class NamedController : Controller

the custom authorizing code

public class AuthoriseIfTableExistsIsTrue : AuthorizeAttribute
{
    private readonly bool _tableExists;

    public AuthoriseIfTableExistsIsTrue()
    {
        _tableExists = string.Equals(bool.TrueString, ConfigurationManager.AppSettings["tableExists"], StringComparison.InvariantCultureIgnoreCase);
    }

    public AuthoriseIfTableExistsIsTrue(bool authorise)
    {
        _tableExists = authorise;
    }

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (_tableExists)
            return base.AuthorizeCore(httpContext);
        else
            throw new HttpException(404, "HTTP/1.1 404 Not Found");
    }
}

I appreciate everyone's assistance and advice to utilize config settings rather than EF for this.

1
5
2/2/2014 10:44:34 AM

Accepted Answer

ZZZ_tmp
7
3/7/2011 10:53:41 AM

Popular Answer

The only options available are

  • Table search with exception
  • Use native SQL to search for that table in system views; with EFv4, you may run queries straight fromObjectContext through telephoneExecuteStoreQuery .

Your entity model will continue to have this table, hence in my view you should just deploy your database with that table and manage whether the functionality is permitted or not in application code (table will not be used but will be in DB).

When a client decides not to utilize a feature, it shouldn't be present in its entirety (including the application code) if you wish to create a modular system.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow