【原创】ASP.NET Core 6 的 Program.cs 中读取配置文件appsettings.json

appsettings.json文件内容

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "OracleConnection": "User Id=MyOracle;Password=OracalPassword;Data Source=192.168.1.1:1521/MyOracle;PERSIST SECURITY INFO=False"
  }
}

Program.cs 文件中,在适当的位置添加如下代码

//启用Oracle数据库
builder.Services.AddDbContext(options =>
{
    var a = new ConfigurationBuilder()
                 .SetBasePath(Directory.GetCurrentDirectory())
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                 .Build()
                 .GetConnectionString("OracleConnection");
    options.UseOracle(a);
});

然后在数据库上下文文件中,为构造函数添加参数

      

你可能感兴趣的:(前端,html,npm,vscode,node.js)