1

I created a .net core 2.1 console app in vs2019. In the DB context OnConfiguring method, I have:

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>

error:

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.
2
  • 1
    See this answer to this question. Though not the accepted answer, I think it may help. It states that you need to install the System.Configuration.ConfigurationManager NuGet package. Commented Aug 10, 2019 at 1:57
  • @EdGibbs Thank you. I got the answer from a different post in that question. Commented Aug 12, 2019 at 15:39

1 Answer 1

1

The problem was I forgot to set App.config to copy to output directory. Got the hint from https://stackoverflow.com/a/46999609/310767.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.