【问题标题】:How to set Oracle connection string for dotNET core console app?如何为 dotNET 核心控制台应用程序设置 Oracle 连接字符串?
【发布时间】:2019-08-09 21:18:30
【问题描述】:

我在 vs2019 中创建了一个 .net core 2.1 控制台应用程序。在 DB 上下文 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.

【问题讨论】:

  • this answerthis question。虽然不是公认的答案,但我认为它可能会有所帮助。它指出您需要安装System.Configuration.ConfigurationManager NuGet 包。
  • @EdGibbs 谢谢。我从该问题的另一篇文章中得到了答案。

标签: oracle console-application asp.net-core-2.1 visual-studio-2019


【解决方案1】:

问题是我忘记将App.config 设置为复制到输出目录。 得到了https://stackoverflow.com/a/46999609/310767的提示。

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 2015-03-02
    • 2020-07-31
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多