Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using connection string from appsettings.json to startup.cs

Currently in Startup, I have my sql server string looking like this:

public void ConfigureServices(IServiceCollection services)
{
    var connection = @"Server=servername;Database=database;Trusted_Connection=True;MultipleActiveResultSets=true";
    services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection));
}

How do I use what's in my appsettings.json:

{
  "Data": {
    "DefaultConnection": {
    "ConnectionString": "Data Source=server;Initial Catalog=database;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

To look something like this in the new ASP.NET 1.0 CORE new setup to look paramertized like this:

public void ConfigureServices(IServiceCollection services)
{
    var connection2 = new SqlConnection connectionString;
    services.AddDbContext<CRAMSContext>(options => options.UseSqlServer(connection2));
}

Also, if I have a different database for the test and qa, how do I let the ASP.NET app know to use a connection for each environment?

My startup class is already defined at the root like this:

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();
}
like image 484
enavuio Avatar asked Nov 28 '16 23:11

enavuio


People also ask

How do I call a Connection String from Appsettings json?

Reading Connection String from AppSettings. json file using IConfiguration interface. In the below example, the IConfiguration is injected in the Controller and assigned to the private property Configuration. Then inside the Controller, the Connection String is read from the AppSettings.

How do I run Appsettings json at startup?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new AppSettings entry is added.

Where do I put Connection String in asp net core?

The connection string should be added to your application's App. config file (Web. config if you are using ASP.NET). If your connection string contains sensitive information, such as username and password, you can protect the contents of the configuration file using Protected Configuration.


1 Answers

Use proper structure for connection strings:

{
  "ConnectionStrings": {
    "DefaultConnection": "xxx"
  }
}

Access in startup.cs:

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
like image 80
sensei Avatar answered Oct 06 '22 00:10

sensei