1

I have my connection string specified in appsettings.json and appsettings.Production.json, as follows:

{
    "ConnectionStrings": {
    "Database": "Host=localhost;Port=5432;Database=<mydb>;Username=<myuser>;Password=<mypw>"
}

(I've obviously removed the secrets from the string)

I then publish the solution. I can run it on the same system with dotnet <PathToThePublishedDLL> just fine. But when I copy the published files over to the Linux server and run it with that dotnet command, I get the error:

System.ArgumentNullException: Value cannot be null.
Parameter name: connectionString
at Npgsql.EntityFrameworkCore.PostgreSQL.Utilities.Check.NotEmpty(String value, 
String parameterName)
at Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsExtensions.UseNpgsql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action`1 npgsqlOptionsAction)
at MapToGlobe.Startup.<ConfigureServices>b__2_0(DbContextOptionsBuilder options) in [path to the solution folder on my dev pc]\Startup.cs:line 25

In the Startup.cs file I have:

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
     services.AddEntityFrameworkNpgsql();
     services.AddDbContext<DatabaseContext>(options => options.UseNpgsql(Configuration.GetConnectionString("Database"))).BuildServiceProvider();
}

Line 25 is the "services.AddDbContext" line.

I've found a number of questions and answers about this, but nothing I have tried has worked and at this point I'm not sure what to do.

Kurt
  • 1,868
  • 3
  • 21
  • 42

1 Answers1

3

Finally found my answer here: https://stackoverflow.com/a/50579414/1078475

It was working from my local machine because, in the command prompt, I was navigating to the folder and running dotnet <mydll>. Whereas on the server I was running it from the root directory with dotnet <pathtomydll>. Because the appsettings file doesn't exist in the root folder, it didn't work.

Kurt
  • 1,868
  • 3
  • 21
  • 42