1

Entity Framework Code First (5.0) を使用して SQL Azure データベースと通信する Azure ワーカー ロールがあります。現在、Worker ロールの app.config に接続文字列がありますが、接続文字列を Woker ロールのロール環境設定に移動して、Azure の再デプロイを必要とせずにライブ サービスの同僚が接続文字列を簡単に変更できるようにしたいと考えています。パッケージ。

現在、次の形式でコンテキストを初期化しています。

protected BaseContext()
        : base("name=DataStore")
{  
    try
            {
                ((IObjectContextAdapter)this).ObjectContext.Connection.Open();

                var storeConnection = (SqlConnection)currentDbConn;
                new SqlCommand("declare @i int", storeConnection).ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                currentDbConn.Close();
                Trace.TraceError("Error occured while getting connection to context", ex.Message);
                throw;
            }
}

RoleEnvironment から接続文字列をプルできるように DbContext(string nameOrConnectionString) をオーバーライドできませんでした。また、新しい SqlConnection を作成し、それを Database.Connection プロパティに割り当てようとしましたが、セッターはありません:/。

これを達成する方法についてのアイデアやガイダンスはありますか?

この質問をご覧いただきありがとうございます。

4

1 に答える 1

2

このようなコンテキストで ContextFactory クラスまたは静的 Factory メソッドを作成することをお勧めします

public class BaseContext : DbContext
{
    public BaseContext(string connectionString)
        : base(connectionString)
    {
    }

    public static BaseContext Create()
    {
        return new BaseContext(
            RoleEnvironment.GetConfigurationSettingValue("connectionString"));
    }
}
于 2013-05-02T01:35:40.187 に答える