djack109
djack109

Reputation: 1377

ASP.NET Core 2.x OnConfiguring get connectionstring string from appsettings.json

Just started messing with ASP.NET Core, pretty impressive so far. In the code generated,(see below). I want to change the hardcoded connection string to get it from the appsettings.json file.

This is apparently impossible. I haven't found a single example that works (or even builds).

What's going on?? Please help

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
            optionsBuilder.UseSqlServer("Server=xxxxxxx;Database=xxxxx;Trusted_Connection=True;");
        }
    }

The link provided solves the problem in one area but doesn't work here in OnConfiguring. What am I doing wrong ?

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.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        var connection = Configuration.GetConnectionString("ConnectionName");
        services.AddDbContext<SurveyContext>(options => options.UseSqlServer(connection));
    }

    // 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();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

Upvotes: 4

Views: 6486

Answers (3)

Neville Nazerane
Neville Nazerane

Reputation: 7019

When you use the Scaffold-DbContext, by default it hard codes your string into the DbContext class (so it works out of the box). You will need to register your DbContext in your startup class to proceed. To set this up, you can check the instructions in this answer.

Note that the Configuration property directly connects to your appsettings.json and several other locations. You can read more about it in this documentation. While you can always use the appsettings.json file, it is generally recommended to have your secure secrets in an external json file outside your source code. The best solution for this during development is using the secret manager. The easiest way to use this is right click on your project on visual studio and select "manage user secrets". This will open a json file that is already connected to your Configuration object.

Once this is set up, you need to use dependency injection to access your db context.

public class HomeController : Controller
{


     public HomeController(SurveyContext context)
     {
         // you can set the context you get here as a property or field
         // you can use visual studio's shortcut ctrl + . when the cursor is on "context"

         // you can then use the context variable inside your actions
     }
}

When you use using, it creates a new connection each time. Using injection makes sure only one connection is created per request no matter how many times it is used.

Upvotes: 1

Karl Fridlund
Karl Fridlund

Reputation: 21

In the startup class of a .NET Core project you usually register this in the ConfigureServices function.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<YourContext>(options => options.UseSqlServer(connection));
}

When you are in the startup class of a .NET Core it is no problem to read the values from appsettings.json.

You could read more at Microsoft, i.e. here: https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

Upvotes: 2

Minu
Minu

Reputation: 242

In the place where you want to access the appsettings.json,

JToken jAppSettings = JToken.Parse( 
      File.ReadAllText(Path.Combine(Environment.CurrentDirectory, 
      "appsettings.json")));

Now since you have the object, you can access its contents. Let me know if it works.

Upvotes: 1

Related Questions