6

4 つのエンティティ データ モデルを持つプロジェクトがあります。それらを構築するために、プロジェクトに接続文字列を保存したくありません。接続文字列をapp.configファイルに保存し、モデル間で共有したいのですが、どうすればよいですか?

ありがとう

4

3 に答える 3

3

最初に DbContext とモデル/データベースを想定しています。

  1. 生成された EF 接続文字列を app.config ファイルにそのまま残します。Arthur が言ったように、EF メタデータ (csdl/ssdl/msl) へのパスが含まれています。これらは、モデル デザイナーによる開発中にも使用されます。
  2. 「SharedConnection」などのストア接続文字列を追加します。これは、本番環境で変更する必要がある唯一の接続文字列です。
  3. DbContext から派生する基本クラスを作成し、そのクラスからすべてのコンテキストを派生させます。
  4. 基本クラスで明示的に既定の EF 接続を作成します。次に、次のように共有接続文字列を使用するように変更します。
public class BaseContext : DbContext
{
    public BaseContext(string nameOrConnectionString)
        : base(CreateConnection(nameOrConnectionString), true)
    {
    }

    private static EntityConnection CreateConnection(string connectionString)
    {
        // Create a (plain old database) connection using the shared connection string.
        DbConnection dbConn = Database.DefaultConnectionFactory.CreateConnection(
            ConfigurationManager.ConnectionStrings["SharedConnection"].ConnectionString);

        // Create a helper EntityConnection object to build a MetadataWorkspace out of the
        // csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
        EntityConnection wsBuilder = new EntityConnection(connectionString);

        // Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
        return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
    }
}

BaseContext から継承する必要があることを除いて、派生コンテキストのコードは変更されません。これは、より堅牢な CreateConnection メソッドです。エラー処理があり、アプリケーション設定を追加するという代償を払って、コードから共有接続文字列の名前を削除します。

private static EntityConnection CreateConnection(string connectionString)
{
    // Find the name of the shared connection string.
    const string appSettingKey = "SharedConnectionStringName";

    string sharedConnectionStringName = ConfigurationManager.AppSettings[appSettingKey];
    if (string.IsNullOrEmpty(sharedConnectionStringName))
    {
        throw new Exception(string.Format(
            "Shared connection not configured. " +
            "Please add a setting called \"{0}\" to the \"appSettings\" " +
            "section of the configuration file.", appSettingKey));
    }

    // Create a (plain old database) connection using the shared connection string.
    ConnectionStringSettings backendSettings =
        ConfigurationManager.ConnectionStrings[sharedConnectionStringName];
    if (backendSettings == null)
    {
        throw new Exception(string.Format(
            "Invalid connection string name \"{0}\" in appSetting \"{1}\"",
            sharedConnectionStringName, appSettingKey));
    }

    System.Data.Common.DbConnection dbConn =
        Database.DefaultConnectionFactory.CreateConnection(
        backendSettings.ConnectionString);

    // Create a helper EntityConnection object to build a MetadataWorkspace out of the
    // csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
    EntityConnection wsBuilder = new EntityConnection(connectionString);

    // Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
    return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
}
于 2013-01-21T13:56:30.700 に答える
1

Code First を使用できます。モデルごとにマッピングをコードで記述する必要がありますが、同じ接続文字列を使用するのは非常に簡単で"name=MyConnectionStringName"、DbContext コンストラクターに渡すだけです。

EDMX で Database First を使用する場合、EF 接続文字列には次の両方が含まれます。

  • データベースへの接続方法に関する情報を提供する「ストア接続文字列」
  • モデルを記述する EF メタデータへのパス

2 番目の部分はモデルごとに異なるため、4 つの異なる接続文字列が必要になります。

接続文字列を 1 つだけにしたい場合は、次のことを行う必要があります。 - メタデータ情報を別の方法で保存する - メタデータをロードして MetadataWorkspace を作成するコードを記述する - 単一のストア接続文字列を読み取り、そこから接続を作成する -接続と MetadataWorkspace の両方を使用して EntityConnection を作成します - EntityConnection を使用して ObjectContext を作成します - DbContext を使用している場合は、ObjectContext を使用して DbContext を作成します

app.config に 4 つの接続文字列を含めるよりも、この重要なコードをすべて記述する方が優れているとは信じがたいようです。

于 2012-04-22T15:22:28.010 に答える
0

EF 接続文字列を ObjectContext に渡すことができます。複数のモデルがある場合は、それぞれの接続文字列を app.config に入れ、それぞれにキーを与え、必要に応じてこれを検索し、インスタンス化するときに適切な文字列をコンテキストに渡すことができます。

于 2012-04-23T04:34:53.063 に答える