检查表使用LINQ数据库中存在 [英] Check if a table exists within a database using LINQ

查看:82
本文介绍了检查表使用LINQ数据库中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个已经被部署到不同的客户数据库。目前,我们引入了一个可选的新功能,使用,需要谁想要的功能,有一个新的表添加到现有的数据库中的客户。

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 feature to have a new table added to the existing database.

由于我们正在推出的一款新软件,将必须与具有和不新表的数据库版本(交互和我们不希望谁拥有新表和一个客户2版本的一个对于那些谁不),我们想知道是否有可能以编程方式确定(实体框架)在数据库中是否存在一个表(我可以尝试访问该表,并把它抛出一个异常,但不知道是否有一个内置函数来做到这一点)

As we are rolling out a new piece of software that will have to interact with versions of the database both with and without the new table (and as we don't want 2 versions one for customers who have the new table and one for ones who don't) we were wondering if it is possible to programmatically determine (with entity framework) whether a table exists in the database (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)

感谢

编辑:由于人们告诉我,我应该使用与EF不检查一个配置文件谁能给我指导做好有关如何检查与配置文件,例如,自定义数据注解为MVC控制器。是这样的:

Given that people are telling me i should be using a config file not checking with EF can anyone give me guidence on how to check the config file with, for example, a custom data annotations for a mvc controller. Something like:

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

会抛出未发现假的,如果一个页面?

Which throws a page not found if false?

编辑2:与人给出的建议使用的配置设置我结束了以下解决方案

Edit 2: With the Suggestions given by people to use the config settings i ended up with the following solution

应用程序设置来设置表是否存在

App settings to set whether the table exists

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

自定义数据注解说是否允许访问控制器

a custom data annotation to say whether to allow access to controller

[AuthoriseIfTableExistsIsTrue]
public class NamedController : Controller

在code为自定义授权

the code for the custom authorise

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");
    }
}

感谢大家的帮助,告诉我不要用EF,这和使用的配置设置,而不是

Thanks everyone for the help and telling me not to use EF for this and use config setting instead

推荐答案

一个更好的选择是将存储的版本差异配置。这可以存储在数据库本身,一个配置文件或者甚至we​​b.config中

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.

否则,你最终会凌乱code,如:

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();

这篇关于检查表使用LINQ数据库中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆