1

I am fairly new to both EF and .NET Core, but using a working project I was able to build a Core 2.0 Web API solution. The only issue is I am unable to get Entity Framework to instantiate on startup. I am receiving an error stating:

System.ArgumentNullException: Value cannot be null. Parameter name: connectionString

My Startup.cs file:

 public class Startup
{
    public Startup(IConfiguration configuration)
    {

        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("dbc")));
        services.AddMvc();
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

My validated JSON file:

  {
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "ConnectionStrings": {
      "dbc": "Server=cc;Initial Catalog=dd;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

UPDATE My Program.Cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

   public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}
3
  • How is the configuration variable (that I'm assuming you are injecting into the Startup function) being created? Is your Configuration property being set? Commented Sep 14, 2017 at 17:12
  • public IConfiguration Configuration { get; } in the startup.cs file Commented Sep 14, 2017 at 17:22
  • just posted an answer, I not sure how your Configuration knows about your appSettings.json Commented Sep 14, 2017 at 17:24

3 Answers 3

1

Try changing your Startup function to

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

to instantiate your configuration and use the appsettings.json file.

Sign up to request clarification or add additional context in comments.

2 Comments

Same error which is being thrown in ConfigureServices()
This isn't the right answer. The default config when you use the 2.0 templates.
0

The ConnectionString must be stored in the file name appsettings.json. Please try to move the Connection string part to the appsettings.json file and try. For e.g.: appsettings.json File

"Data": {
  "dbc": {
      "ConnectionString": "Server=cc;Initial Catalog=dd;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
}

Startup.cs File

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:dbc:ConnectionString"]));

1 Comment

My JSON is in the appsettings.json file. As a test I used your format and still received the same error.
0

Oddly enough, moving ConnectionStrings to the top of the apsettings.json file did the trick. Thank you all for your help.

 {
      "ConnectionStrings": {
        "dbc": "Server=cc;Initial Catalog=dd;Trusted_Connection=True;MultipleActiveResultSets=true"
      },
      "Logging": {
        "IncludeScopes": false,
        "Debug": {
          "LogLevel": {
            "Default": "Warning"
          }
        },
        "Console": {
          "LogLevel": {
            "Default": "Warning"
          }
        }
      }
    }

3 Comments

did that work with your original startup code? Also, your json looks off for the Logging section which I think is why it works to move the ConnectionStrings section up.
The logging section was definitely off which was the root cause. Oddly enough both variations validated using JSONlint. I fixed the syntax and now I can move the connectionstrings section around.
Yep, I guess structurally it was valid JSON, it just didn't make sense. Glad it's working for you now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.