-1

I have Windows 10 computer. I am using Visual Studio 2019.

I created a solution MySolution.

Inside the solution, first I created a .NET Core Library project named MyLibrary. Via Nuget Package Manager, I added System.Configuration.ConfigurationManager (5.0.0 version) and System.Data.SqlClient (4.8.2 version) packages to it.

I add an Application Configuration File named App.Config to it.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MyConnectionString" connectionString="Server=myserver;Initial Catalog=mydatabase;Trusted_Connection=True; Pooling=true; Min Pool Size=10;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

In this project I created a static class named DatabaseInformation. Inside that class I created a static constructor which is reading the configuration file:

static DatabaseInformation()
{
   ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["MyConnectionString"];
}

Inside the solution, I then created a .NET Core Console App project named MyConsoleApp to test out the MyLibrary project. I added MyLibrary project's reference to it. Then write code to trigger the static constructor of DatabaseInformation class. Run in debug mode and find out it is not reading the config file, and the settings variable is null.

What could be the reason and how can I fix it? I can see the config file as MyLibrary.dll.config in \bin\Debug\netcoreapp3.1 folder so it should have been read.

srh
  • 1,661
  • 4
  • 30
  • 57

2 Answers2

1

I think you need to add the App.Config file to the main solution and not in the class library project just like @KenTsu posted the comment to another stack question that says the same thing.

Josh
  • 188
  • 1
  • 12
1

In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings.json, an environment variable, the user secret store, or another configuration source. See the Configuration section of the ASP.NET Core documentation for more details.

For instance, you can use the Secret Manager tool to store your database password and then, in scaffolding, use a connection string that simply consists of Name=<database-alias>.

dotnet user-secrets set ConnectionStrings.YourDatabaseAlias "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=YourDatabase"
dotnet ef dbcontext scaffold Name=ConnectionStrings.YourDatabaseAlias Microsoft.EntityFrameworkCore.SqlServer

Or the following example shows the connection string stored in appsettings.json.

{
  "ConnectionStrings": {
    "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
  },
}

Then the context is typically configured in Startup.cs with the connection string being read from configuration. Note the GetConnectionString() method looks for a configuration value whose key is ConnectionStrings:<connection string name>. You need to import the Microsoft.Extensions.Configuration namespace to use this extension method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<BloggingContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}
MD. RAKIB HASAN
  • 3,670
  • 4
  • 22
  • 35