WPF Core - Connection Strings inside App.config Not Working

Faraz Qureshi 116 Reputation points
2020-03-28T19:56:14.063+00:00

Upon facing the said issue even followed the simple walkthrough at Connection Strings - EF Core but found out that while hardcoding the connectionstring as:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
{  
    optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");  
}  

leads to success, using the recommended approach of inserting an App.config file like:

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <connectionStrings>  
    <add name="BloggingDatabase"  
         connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" providerName="System.Data.SqlClient" />  
  </connectionStrings>  
</configuration>  

and updating the main code to:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
{   optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);  
}  

is not successful, and following error being faced while carrying out migration:

System.NullReferenceException: Object reference not set to an instance of an object.  
   at Intro.BloggingContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\<...FileLocation...>\AppDbContext.cs:line 14  
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()  
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()  
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)  
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.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.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)  
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)  
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()  
   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.  

What could be the reason behind the same?

Strange enough! Upon using the index technique i.e. like:

optionsBuilder.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString);  

is successful but regretfully doesnot allow to update or remove-migration on the following complaint:

System.ArgumentException: Invalid value for key 'attachdbfilename'  

Thanks in advance for your reviewing and replying.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-03-30T05:53:19.69+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    You need to make sure that the programName.exe.config file is generated in the bin directory so that ConfigurationManager can read it.

    Thanks.

    1 person found this answer helpful.

  2. Lex Li (Microsoft) 4,662 Reputation points Microsoft Employee
    2020-05-25T01:33:04.717+00:00

    .NET Core applications do not honor app.config at all, but use different ways for configuration,

    https://learn.microsoft.com/en-us/dotnet/core/run-time-config/

    0 comments No comments

  3. Duane Arnold 3,211 Reputation points
    2020-05-28T18:57:28.93+00:00
    0 comments No comments