CS0120 非静态字段、方法或 属性 'Startup.Configuration' 需要对象引用
CS0120 An object reference is required for the non-static field, method, or property 'Startup.Configuration'
我正在使用 .NET Core 3.0.1 并在 Services.cs
中设置我的 dbcontext。我使用了以下代码,但显示了此错误:
An object reference is required for the non-static field, method, or
property 'Startup.Configuration'
但教程显示相同的代码。
谁能看出我可能哪里出错了?
services.AddDbContext<IPSLContext>(options => options.UseSqlServer(Configuration.GetConnectionString("myDb")));
string connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<IPSLContext>(options => options.UseSqlServer(connectionString));
完整的 .cs 文件
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?}");
});
}
}
}
问题是 public IConfiguration Configuration { get; }
不是静态的,但试图调用它的 ConfigureServices
是静态的。
只需使用 public void ConfigureServices(IServiceCollection services)
(注意没有静态修饰符)。
我正在使用 .NET Core 3.0.1 并在 Services.cs
中设置我的 dbcontext。我使用了以下代码,但显示了此错误:
An object reference is required for the non-static field, method, or property 'Startup.Configuration'
但教程显示相同的代码。
谁能看出我可能哪里出错了?
services.AddDbContext<IPSLContext>(options => options.UseSqlServer(Configuration.GetConnectionString("myDb")));
string connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<IPSLContext>(options => options.UseSqlServer(connectionString));
完整的 .cs 文件
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?}");
});
}
}
}
问题是 public IConfiguration Configuration { get; }
不是静态的,但试图调用它的 ConfigureServices
是静态的。
只需使用 public void ConfigureServices(IServiceCollection services)
(注意没有静态修饰符)。