如何为 dotNET 核心控制台应用程序设置 Oracle 连接字符串?

How to set Oracle connection string for dotNET core console app?

我在 vs2019 中创建了一个 .net core 2.1 控制台应用程序。在数据库上下文 OnConfiguring 方法中,我有:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    var conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConsoleApp1Core"].ConnectionString; // this does not work
    var conn = "User Id=xxx;Password=xxx;Data Source=xxx:xxx/xxx"; // this works
    optionsBuilder.UseOracle(conn);
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ConsoleApp1Core" connectionString="User Id=xxx;Password=xxx;Data Source=xxx:xxx/xxx" />
  </connectionStrings>
</configuration>

错误:

PM> get-dbcontext
System.NullReferenceException: Object reference not set to an instance of an object.
   at ConsoleApp1Core.Program.BloggingContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\ConsoleApp1Core\Program.cs:line 21
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.GetContextInfo(String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetContextInfoImpl(String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

问题是我忘记设置App.config复制到输出目录。 从 得到提示。