12

実行時にデータベースの接続文字列を提供したいと考えています。Entity Framework を使用しています。これは私がこれまでに持っているものです

class MyClassDBContext:DbContext
{
  public MyClassDBContext(string str) : base(str)
  {
    this.Database.Connection.ConnectionString = str;
  }
}

上記のコードを使用するために、試しました

//create connection string
EntityConnectionStringBuilder myConn = new EntityConnectionStringBuilder();
myConn.Provider = "System.Data.SqlClient";
myConn.ProviderConnectionString = "user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30";

//inject the connection string at runtime
MyClassDBContext a = new MyClassDBContext(myConn.ToString())

上記のコードでは、「プロバイダーのキーワードがサポートされていません」というエラーが表示されました。このエラーをデバッグするために、次のことを試しました

 MyClassDBContext a = new MyClassDBContext("metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30")

今、「メタデータキーワードはサポートされていません」というエラーが表示されました。だから私は自分のコードを

MyClassDBContext a = new MyClassDBContext("provider=System.Data.SqlClient;provider connection string=user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30")

「プロバイダーキーワードがサポートされていません」というエラーが表示されました。だから私は再び私のコードを

 MyClassDBContext a = new MyClassDBContext("user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30")

そして今、それは動作します!. 私の質問は、実行時にプロバイダーとメタデータを指定するにはどうすればよいですか? 接続文字列のみが受け入れられているようです。Nuget の Entity 4.3.1 を使用しています。

ありがとう

4

5 に答える 5

13

edmx ファイル ベースの EF には、「プロバイダー」と「メタデータ」のコンテンツが必要です。コード ファースト ベースの EF ではこれは必要なく、通常の接続文字列のみが必要です。SqlConnectionStringBuilder必要に応じて、 (の代わりに) を使用して、EntityConnectionStringBuilderこの通常の接続文字列を作成できます。しかし、これまで見てきたように、実際の接続の詳細を指定するだけで済みます。Provider と Metadata は、EF 4.3.1 の DbContext Code-first パラダイムでは必要ありません。

于 2012-07-06T21:07:53.837 に答える
10

HatSoftの答えに基づいて構築:

var entityConnectionStringBuilder= new EntityConnectionStringBuilder();
entityConnectionStringBuilder.Provider = "System.Data.SqlClient";
entityConnectionStringBuilder.ProviderConnectionString = <your SQL Server connection string>;
entityConnectionStringBuilder.Metadata = "res://*";

MyClassDBContext a = new MyClassDBContext(entityConnectionStringBuilder.ToString());
于 2013-05-21T20:28:36.750 に答える
7

EntityConnectionStringBuilder クラスを使用して、実行時にプロバイダーとメタデータを指定できます

例えば

var entityConnectionStringBuilder= new EntityConnectionStringBuilder(); entityConnectionStringBuilder.Provider = "System.Data.SqlClient"; entityConnectionStringBuilder.Metadata = "res:// /Example.csdl|res:// /Example.ssdl|res://*/Example.msl";

CSDL、SSDL、および MSDLの詳細については、メタデータを参照してください。

于 2012-07-06T20:15:06.957 に答える
4

このリンクをたどった

そしてこれも

app.conf ファイルなしで EF Code-First を使用するには?

基本的に私がしていることはほとんどあなたと同じで、文字列でコンストラクターを作成し、ベースを呼び出します。

ただし、このコンストラクターにはプロバイダーも設定しています。

これが例です

public Context(string ConnectionString) : base(ConnectionString) { 
    Database.DefaultConnectionFactory = new SqlCeConnectionFactory("Oracle.DataAccess.Client"); 
}

これにより、プロバイダーを指定できます。また、接続文字列で指定する必要がないため、プロバイダー キーワード エラーは発生しません。

これが私がそれを呼ぶ方法です。

var dbCont = new ClassLibrary1.Models.Context("DATA SOURCE=xxx;PASSWORD=xxx;USER ID=xxx");

それを見つけるのに長い時間がかかったのを助けてくれることを願っています

于 2013-04-05T22:27:03.090 に答える
2

それは古い質問ですが、誰かにとって役立つかもしれません

 var provider = (DbProviderFactory)System.Data.Entity.DbConfiguration
            .DependencyResolver
            .GetService(typeof(DbProviderFactory), "invariant provider name");

        var conn = provider.CreateConnection();
        //conn.ConnectionString = "sample connection string";

        DbInterception.Dispatch.Connection.SetConnectionString(conn, new DbConnectionPropertyInterceptionContext<string>()
            .WithValue("sample connection string"));

return new SampleDbContext(conn,true);
于 2016-08-24T11:28:59.497 に答える