2012-08-07 24 views
15

オブジェクトコンテキストデータモデル(EDMXファイルを使用)を使用している場合は、作成中に、設定ファイル内に接続文字列を指定できます。エンティティフレームワークからのプロバイダ接続文字列

残念ながら、エンティティ接続に必要なものが含まれているため、接続文字列は一般的な接続文字列ではありません。 MySQL接続と例:

<add name="MyDbEntities" connectionString="metadata=res://*/Namespace.MyDb.csdl|res://*/Namespace.MyDb.ssdl|res://*/Namespace.MyDb.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=172.17.17.154;User Id=user;password=password;Persist Security Info=True;database=MyDatabase;Convert Zero Datetime=true&quot;" providerName="System.Data.EntityClient" /> 

私が持っている問題は、この接続文字列は、パラメータ「プロバイダの接続文字列」で、プロバイダの接続文字列が含まれていることです。

具体的な理由から、エンティティモデルとは無関係に新しいMySqlConnectionを作成する必要があります。 MySqlConnectionを作成するには、mysql接続文字列を提供する必要があります。これはエンティティモデルのプロバイダ接続文字列であり、必要な接続文字列は常にエンティティモデルと同じ接続文字列です。

しかし、どうすればプロバイダ接続文字列をプログラマティックに得ることができますか?

ModelInstance.Connection.ConnectionString 

は「名前= TestBotEntities」ではなく、さらに全体の接続文字列のようなものが含まれています。私は

次...ない成功を収めたモデルインスタンスを閲覧して貼り付けました。

ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString 

をそれ1は全体のエンティティの接続文字列が含まれていると私はちょうどそれからの唯一のプロバイダの接続文字列を取得する方法、それを解析する方法がわからない。だから私は試してみました。

答えて

33

2つの方法があります。

私はEntityConnectionStringBuilderを経由して、エンティティの接続文字列を解析することができます:

string entityConnectionString = ConfigurationManager.ConnectionStrings["MyDbEntities"].ConnectionString; 
string providerConnectionString = new EntityConnectionStringBuilder(entityConnectionString).ProviderConnectionString; 

を...または、私が利用可能な特定のモデルのインスタンスを持っている場合、私はここからそれを得ることができます。

((System.Data.EntityClient.EntityConnection)ModelInstance.Connection).StoreConnection.ConnectionString; 
関連する問題