0

I added a Startup.cs to my .NET 6 console application as described here. To avoid having to manually specify the connection string when calling Scaffold-DbContext, I tried to provide the connection string from the application configuration like this:

Scaffold-DbContext -Connection name=DatabaseConnectionName -Provider...

For some reason, this does not seem to work and I get the following error:

System.InvalidOperationException: A named connection string was used, but the name 'DatabaseConnectionName' was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.

Here is the content of my Startup.cs:

public class Startup
{
    IConfiguration Configuration { get; }

    public Startup()
    {
        IConfigurationBuilder builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEntityFrameworkNpgsql().AddDbContext<DatabaseContext>(opt =>
            opt.UseNpgsql(Configuration.GetConnectionString("DatabaseConnectionName")));

        services.AddSingleton(Configuration);
    }
}

And this is the content of my appsettings.json:

{
  "ConnectionStrings": {
    "DatabaseConnectionName": "User ID=...;Password=...;Server=...;Port=5432;Database=database-name;Integrated Security=true;Pooling=true"
  }
}

I noticed that this error only occurs while trying to execute the Scaffold-DbContext command, but not when I am running the application normally. I think that this problem might be related to the fact that I am manually creating an instance of the Startup class in my Program.cs, but I don't know if that is true or what I can do about this.

Is there any way how I can fix this error and let the command read the connection string from the configuration file?

Chris
  • 1,417
  • 4
  • 21
  • 53
  • 1
    To be complete your question should add the relevant information from _appsettings.json_ where the connectionstring should be present. – Steve May 03 '22 at 12:49
  • @Steve I just added it to my question :) – Chris May 03 '22 at 12:49
  • 3
    Are you shure the applictation uses the file you think it does? Ist the file in the Output folder? Are there any similar files like appsettings.Production.json? Maybe debug your application an take a look what's inside Configuration. – keuleJ May 03 '22 at 12:59
  • @keuleJ I can read the connection string at runtime just fine from the configuration, but it does not work while trying to execute the `Scaffold-DbContext` command. I think that my `Startup.cs` is not being recognized in that case because the entry point of my application is not executed, but I have no idea if that's true or what I can do about it. – Chris May 03 '22 at 13:25
  • 2
    The scaffold command has a `-StartupProject` switch that you can use to be sure it uses the right project. – Crowcoder May 03 '22 at 13:28
  • 1
    @Crowcoder I just tried to manually specify the project via the `-StartupProject` switch but I still get the same error. I think that this problem is not related to the wrong startup project being used, but rather because of how I implemented the `Startup.cs`. – Chris May 03 '22 at 13:30
  • Can you try ````Configuration.GetConnectionString("ConnectionStrings.DatabaseConnectionName")```` – Onurkan Bakırcı May 03 '22 at 20:37
  • @OnurkanBakırcı That code does not seem to return the connection string (it only returns null). Configuration.GetConnectionString("DatabaseConnectionName") works fine, though. – Chris May 06 '22 at 08:39

0 Answers0