実行時にデータベースの接続文字列を提供したいと考えています。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 を使用しています。
ありがとう