エンティティデータベースコンテキストを取得する静的クラスを作成することをお勧めします。EF 4.0では、dbcontextを取得する静的クラスは良い方法ですか?
このGetEntity()
は、コンテキストを返します。 GetEntity
メソッドでは、私は動的接続を持っています。 ログインページに行くと、データベース番号+ユーザ名+パスワードを入力する必要があります。私はSession["DBName"]
にdbnameを保管しています。
public static class EntityFactory
{
public static DBEntities GetEntity()
{
var scsb = new SqlConnectionStringBuilder();
scsb.DataSource = ConfigurationManager.AppSettings["DataSource"];
scsb.InitialCatalog = "db1";
scsb.MultipleActiveResultSets = true;
scsb.IntegratedSecurity = true;
if (HttpContext.Current.Session["DBName"] == null)
{
HttpContext.Current.Response.Redirect("/Account/Step1");
}
else
{
scsb.InitialCatalog = HttpContext.Current.Session["DBName"].ToString();
}
var builder = new EntityConnectionStringBuilder();
builder.Metadata = "res://*/nms.bin.Models.DBModel.csdl|res://*/nms.bin.Models.DBModel.ssdl|res://*/nms.bin.Models.DBModel.msl";
builder.Provider = "System.Data.SqlClient";
builder.ProviderConnectionString = scsb.ConnectionString;
DBEntities db = new DBEntities(builder.ConnectionString);
return db;
}
私はのcontrolerの例でDBContextを取得したい場合は、私はちょうどEntityFactory.GetEntity()
を行う必要があり、それは私のDBコンテキストを返します。
- それは私がこの
- のやり方は、20台のクライアントが同時にしかし異なるdbnameのようにしてログインする場合には問題になる可能性です正しいです。
- 現在、私は処分を使用していません、それは問題ですか?私の
EntityFactory
クラスに基づいて、自動的に呼び出されるそのクラス内のグローバルディスポーザブルを作成できますか? (私はdescrutorメソッドについて考える)。順番に
@Erangaは編集に大変感謝しています。私の英語は良くありません:) –