私はシンプルなWebアプリケーションを開発していますが、将来はマルチテナントとしてやりたいと思います。.NETコアget appsettings.jsonから
だから私はストレートOnConfiguring
メソッドへの接続文字列を書きたい:
public class ApplicationContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("connection string from appsettings.json");
base.OnConfiguring(optionsBuilder);
}
}
スタートアップクラス:私はApplicationContext
クラスにappsettings.json
から接続文字列を抽出することができますどのように
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationContext>();
services.AddMvc();
}
?
ApplicationContext
クラスのコンストラクタを作成したくありません。
あなたはそれを 'OnConfiguring'メソッドを登録したいのはなぜAddDbContext IIRC –
にパラメータとして接続文字列を渡す必要があります'services.AddDbContext(options => options.UseSqlServer(" ... "))'ではなく? –
Tseng
@Tsengマルチテナントのために動的に接続文字列を変更する必要があります。 –