CS0120 An object reference is required for the non-static field, method, or property 'Startup.Configuration'

.net-core .net-core-3.1 entity-framework-core

Question

I'm using .NET Core 3.0.1 and to setup my dbcontext in Services.cs. I've used the below code but this errors is shown:

An object reference is required for the non-static field, method, or property 'Startup.Configuration'

but tutorials show the same code.

Can anyone see where I could be going wrong?

services.AddDbContext<IPSLContext>(options => options.UseSqlServer(Configuration.GetConnectionString("myDb")));
string connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<IPSLContext>(options => options.UseSqlServer(connectionString));     

Full .cs file

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SecurityMaintenance.Models;

namespace SecurityMaintenanceCore
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public static void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddAuthentication()
                    .AddCookie(options =>
                     {
                         options.LoginPath = "/Home/UnauthorizedAccess/";
                         options.AccessDeniedPath = "/Home/UnauthorizedAccess/";
                     });
            services.AddMvc(); 
            string connectionString = Configuration.GetConnectionString("myDb");
            services.AddDbContext<IPSLContext>(options => options.UseSqlServer(connectionString));
        }
        public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Removed}/{id?}");
            });
        }
    }
}
1
0
12/13/2019 11:48:04 AM

Accepted Answer

The thing is that public IConfiguration Configuration { get; } is not static but ConfigureServices which tries to call it is. Just use public void ConfigureServices(IServiceCollection services) (note the absence of static modifier).

3
12/13/2019 12:12:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow