entity-framework - Azure Web 角色中的 Entity Framework DbContext

标签 entity-framework azure web-config entity-framework-5 azure-web-roles

我正在将现有 Web 应用程序(使用 Entity Framework 5)迁移到 Azure Web 角色。

数据库连接字符串正在从 web.config 移动到 ServiceConfiguration.*.cscfg 文件。

问题是在自动生成 Model.Context.cs 文件中,我的实体类定义如下:

public partial class MyEntities : DbContext
{
    public MyEntities()
        : base("name=MyEntities")
    { }

    // DbSets, etc
}

这将始终在 web.config 中查找 MyEntities。如何重写此构造函数,以便可以从 ServiceConfiguration.*.cscfg 文件传入连接字符串?

我可以从这个类派生,如下所示:

public class MyCloudEntities : MyEntities
{
    public MyCloudEntities()
        : base(CloudConfigurationManager.GetSetting("MyEntities"))
    { }
}

但是我必须更改代码库中 MyEntities 的每个实例,并且它不会阻止开发人员将来使用 MyEntities

最佳答案

您可以更改 Model.Context.tt 文件以使用

CloudConfigurationManager.GetSetting("MyEntities")

代替

"name=MyEntities"

对于我的实体

因此,每次重新创建上下文时,您都将始终得到更改。在这种情况下,您不需要更改任何其他内容。

关于entity-framework - Azure Web 角色中的 Entity Framework DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17679088/

相关文章:

.net - 如何 : Entity framework and multiple schemas

amazon-web-services - 对 Google Cloud Vision、Microsoft Azure 计算机视觉 API 或 Amazon Text Extract 进行微调

Azure Kubernetes - 每个节点仅执行一次部署?

asp.net - 在Web.config中是否可以注册指定目录中的所有用户控件

web-config - Sitecore:有效的项目名称

Sharepoint Web 部件 : type could not be found/registered as safe

entity-framework - 一个项目中的多个数据库- Entity Framework ( self 跟踪实体)

c# - 将实体列表保存到数据库 - MVC

c# - EntityFramework 将新对象添加到集合中

azure - 通过 VPN 连接到 Azure SQL