我正在使用.NET Core 3.0.1,并在services.AddDbContext<IPSLContext>(options => options.UseSqlServer(Configuration.GetConnectionString("myDb")));
中设置我的dbcontext。我使用了以下代码,但显示了此错误:
非静态字段,方法或 属性“ Startup.Configuration”
但是教程显示了相同的代码。
谁能看到我要去哪里错了?
string connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<IPSLContext>(options => options.UseSqlServer(connectionString));
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?}");
});
}
}
}
.cs完整文件
Group
答案 0 :(得分:2)
问题是public IConfiguration Configuration { get; }
不是静态的,而试图调用它的ConfigureServices
是静态的。
只需使用public void ConfigureServices(IServiceCollection services)
(请注意没有static修饰符)。