question

faraz-qureshi avatar image
faraz-qureshi asked ·

WPF Core - Connection Strings inside App.config Not Working

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
AlexLi-MSFT answered ·

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.

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your looking into the said case AlexLi!

Such a file (i.e. named EFGetStarted.exe.config) is that I find to be missing upon debug/build in case of WPF Net Core.

While the said kind of file no doubt is found to be generated in a case like that of WPF Net Framework.
Besides that, without a migration being successfully carried out and the relevant database created, definitely a program is of no use.

A comprehensive database model tutorial & documentation of some successful project (like the awesome material in case of ASP Core) is required in case of WPF & WinForm Core, Without which sure do find the said edition to be quite immature.

0 Votes 0 · ·

Sorry for my late reply.Do you solve the issue?The issue is because the config file is missing?

0 Votes 0 · ·
lextm avatar image
lextm answered ·

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

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

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
DuaneArnold-0443 answered ·
Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.