【问题标题】:Customize DbContext using connection string from ServiceConfiguration (Azure)使用 ServiceConfiguration (Azure) 中的连接字符串自定义 DbContext
【发布时间】:2014-12-05 14:12:22
【问题描述】:

我正在使用带有代码优先方法的实体框架,并且我有一个自定义 DbContext。 我的连接字符串在 ServiceConfiguration.Cloud.cscfg 和 ServiceConfiguration.Local.cscfg 文件中定义。

 public CustomContext()
                : base("dbActivity")
 {
    ...

当上述代码执行时,它会尝试检查我的 web.config 而不是 Azure 解决方案中的 ServiceConfiguration 文件。

更新: 这是我在 ServiceConfiguration 文件中的 connectionString:

<ConfigurationSettings>
  <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
  <Setting name="dbActivity" value="Server=tcp:xxx.database.windows.net,1433;Database=ra;User ID=user;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" />
</ConfigurationSettings>

例外:

没有名为“Server=tcp:xxx.database.windows.net,1433;Database=ra;User ID=user;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;”的连接字符串可以在应用程序配置文件中找到。”

是否可以改为检查 ServiceConfiguration.Local.cscfg?

【问题讨论】:

标签: c# entity-framework azure


【解决方案1】:

我终于找到了解决方案。需要用到SqlConnectionStringBuilder类。

public RAActivityContext()
    : base((new SqlConnectionStringBuilder(
            CloudConfigurationManager.GetSetting("dbRAActivity")).ToString()))
{            

}

【讨论】:

    【解决方案2】:

    有一个重载的构造函数接受一个实际的连接字符串。

    这应该可以解决问题:

    public CustomContext()
                    : base(CloudConfigurationManager.GetSetting("dbActivity"))
    {
        // stuff here
    }
    

    当然,设置包含(完整的)连接字符串。

    【讨论】:

    • 我之前尝试过这个解决方案,但我仍然遇到同样的异常。看起来它只接受连接字符串名称,而不是连接字符串本身
    • 抛出的异常是什么?你用的是什么版本的EF?您的ServiceConfiguration.Local.cscfg 中的配置是什么样的?我正在执行上述操作,并且在 EF 6 上运行良好。
    • WebRole 还是 WorkerRole? EF 版本?
    • 我正在使用 EF6 和 WorkerRole
    【解决方案3】:

    我不知道你可以直接从不同的配置加载它,但是有一个用于 dbcontext 的构造函数,它接受一个连接字符串作为输入。

    string connectionString = LoadFromConfig();
    var context = new CustomContext(connectionString );
    

    【讨论】:

      【解决方案4】:

      我需要这样做,但无法更改实际的 DbContext 构造函数,因为它位于共享库中,该库也被使用 app.config 的项目使用。我最终想出了一个替代解决方案,即以编程方式修改辅助角色本身内的 app.config 连接字符串。

      查看此方法以了解如何做到这一点:Change connection string & reload app.config at run time

      我使用的代码是

      string databaseConnectionString = CloudConfigurationManager.GetSetting("dbActivity");
      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection) config.GetSection("connectionStrings");
      connectionStringsSection.ConnectionStrings["dbActivity"].ConnectionString = databaseConnectionString;
      config.Save();
      ConfigurationManager.RefreshSection("connectionStrings");
      

      为此,您需要在 app.config 中有一个空的连接字符串,它将覆盖该字符串

      <connectionStrings>
          <add name="dbActivity" connectionString="" providerName="System.Data.SqlClient" />
      </connectionStrings>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-02
        • 2021-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        相关资源
        最近更新 更多