私は、以下のいずれかのようDbContextから派生したコンテキストを持っている:パス接続文字列最初
public class StudentContext : DbContext
{
public StudentContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DBModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentContext, StudentMigrations.Configuration>());
}
public DbSet<Students> Students {get; set;}
}
私がして、接続文字列を渡すためにしようとしている:
studentContext = new StudentContext(settings.ConnectionString)
設定は、コンフィギュレーションファイルを読むことによって実行時にロードされます。 私はthisを試してみましたが、StudentContextコンストラクタ内で接続文字列をthis.Database.Connection.ConnectionStringを使用して設定しようとしました。どちらの場合でも、デフォルトのコンストラクタを提供するか、 IDbContextFactoryの私は私が仕事をする最初のオプションを取得することができれば、コード内で、したがって、静の使用を削減しようとしています
public class StudentContext : DbContext
{
public static string ConnectionString;
public StudentContext(string connectionString) : base(ConnectionString = connectionString)
{
}
//And also provide a default implementation of the DbContext constructor:
public StudentContext() : base(ConnectionString)
{
}
protected override void OnModelCreating(DBModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentContext, StudentMigrations.Configuration>());
}
public DbSet<Students> Students {get; set;}
}
、それは素晴らしいことだ:働く唯一の事はこれです。