如何简单地阅读 appsettings.json
How to simply read from appsettings.json
我有appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionString": "Server=...;Database=...;Trusted_Connection=True;"
}
和数据库 class
public class Database : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=...;Database=...;Trusted_Connection=True;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Material>().ToTable("vw_Material", schema: "query");
}
public virtual DbSet<Material> Material { get; set; }
}
我的问题是,如何替换这一行
optionsBuilder.UseSqlServer(@"Server=...;Database=...;Trusted_Connection=True;");
使用来自应用程序设置的值?
您可以使用 DI 来达到这个目的。为此,您需要在 Startup.cs:
中注册配置
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IConfiguration>(Configuration);
}
然后你可以将它注入数据库构造函数:
private readonly IConfiguration configuration;
public Database(IConfiguration config)
{
configuration = config;
}
并在配置时访问它:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(configuration.GetConnectionString("ConnectionString"));
另一种选择是使用选项模式。在这种情况下,您必须使用您需要的 属性 创建一个 class:
public class ConnectionStringConfig
{
public string ConnectionString { get; set; }
}
启动时注册:
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<ConnectionStringConfig>(Configuration);
}
注入数据库构造函数:
private readonly ConnectionStringConfig configuration;
public Database(IOptions<ConnectionStringConfig> config)
{
configuration = config.Value;
}
并在配置时访问它:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(configuration.ConnectionString);
我有appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionString": "Server=...;Database=...;Trusted_Connection=True;"
}
和数据库 class
public class Database : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=...;Database=...;Trusted_Connection=True;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Material>().ToTable("vw_Material", schema: "query");
}
public virtual DbSet<Material> Material { get; set; }
}
我的问题是,如何替换这一行
optionsBuilder.UseSqlServer(@"Server=...;Database=...;Trusted_Connection=True;");
使用来自应用程序设置的值?
您可以使用 DI 来达到这个目的。为此,您需要在 Startup.cs:
中注册配置public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IConfiguration>(Configuration);
}
然后你可以将它注入数据库构造函数:
private readonly IConfiguration configuration;
public Database(IConfiguration config)
{
configuration = config;
}
并在配置时访问它:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(configuration.GetConnectionString("ConnectionString"));
另一种选择是使用选项模式。在这种情况下,您必须使用您需要的 属性 创建一个 class:
public class ConnectionStringConfig
{
public string ConnectionString { get; set; }
}
启动时注册:
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<ConnectionStringConfig>(Configuration);
}
注入数据库构造函数:
private readonly ConnectionStringConfig configuration;
public Database(IOptions<ConnectionStringConfig> config)
{
configuration = config.Value;
}
并在配置时访问它:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(configuration.ConnectionString);