0

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?

Thanks in advance for your reviewing and replying.

  • Did you check this properties `ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString`? – Pavel Anikhouski Mar 28 '20 at 15:18
  • Does this answer your question? [Is ConfigurationManager.AppSettings available in .NET Core 2.0?](https://stackoverflow.com/questions/47591910/is-configurationmanager-appsettings-available-in-net-core-2-0) – Pavel Anikhouski Mar 28 '20 at 15:19
  • If you are referring to use of the namespace **System.Configuration** I have done the same plus the said property being completely being referred to as the following does not resolve the said problem: optionsBuilder.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString); – Faraz Ahmed Qureshi Mar 28 '20 at 16:31
  • **Strange!!!** Upon using the index technique i.e. like: **optionsBuilder.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString);** is successful! – Faraz Ahmed Qureshi Mar 28 '20 at 17:06

2 Answers2

0

Finally found the only satisfying answer to a mirror question here.

App.Config is not a correct choice for Connection Strings for WPF Core. It's rather an appsettings.json file successful enough in this regard.

0

Strange!!! Upon using the index technique i.e. like: optionsBuilder.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString); is successful! – Faraz Ahmed Qureshi Mar 28 at 17:06

Indeed this works

Somang
  • 33
  • 6