我们有一个已经部署在不同客户端的数据库。我们正在推出一项可选的新功能,要使用该功能,需要将新表添加到现有数据库中的客户端。
自定义授权的代码
感谢大家的帮助,告诉我不要使用EF来做这件事,而是使用配置设置。
由于我们正在推出一款新软件,该软件必须与具有和不具有新表版本的数据库进行交互(并且我们不希望为具有新表和不具有新表的客户分别提供两个版本),因此我们想知道是否可以通过编程方式确定(使用实体框架)数据库中是否存在表(我可以尝试访问该表并使其抛出异常,但是想知道是否有内置函数可以完成此操作)。
谢谢
编辑:鉴于有人告诉我应该使用配置文件而不是使用EF进行检查,请问有人可以指导我如何检查配置文件吗?例如,在MVC控制器中使用自定义数据注释的某些内容:
[Boolean(Properties.Settings.Default.TableExists)]
public class NamedController : Controller
如果为假,哪个会抛出“页面未找到”的错误?
编辑2:根据人们提供的建议使用配置设置,我最终得出了以下解决方案
应用程序设置用于设置表是否存在
<appSettings>
<add key="tableExists" value="True"/>
</appSettings>
一个自定义数据注释,用于指定是否允许访问控制器。
[AuthoriseIfTableExistsIsTrue]
public class NamedController : Controller
自定义授权的代码
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来做这件事,而是使用配置设置。