System.InvalidOperationException:访问DbContext时
System.InvalidOperationException: when accessing DbContext
我在 运行 我的 .Net Core 应用程序时遇到错误。我开始访问 DbContext 并弹出
System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.'
我已经尝试修复它,但它仍然出现。
DbContext.cs
public class PartsDbContext : DbContext
{
public DbSet<Tower> Towers { get; set; }
public DbSet<Motherboard> Motherboards { get; set; }
public PartsDbContext(DbContextOptions<PartsDbContext> options)
: base(options)
{
}
}
Controller.cs
public class AdminController : Controller
{
private readonly PartsDbContext _context;
public AdminController(PartsDbContext context)
{
_context = context;
}
public IActionResult Index()
{
if (User.Identity.Name == null)
{
return RedirectToAction("Register", "Account");
}
return View();
}
public IActionResult Towers()
{
var model = _context.Towers.ToList();
return View(model);
}
public IActionResult Motherboards()
{
var model = _context.Motherboards.ToList();
return View(model);
}
}
错误出现在 Towers() 的这一行
var model = _context.Towers.ToList();
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddEntityFramework()
.AddDbContext<PartsDbContext>();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
任何帮助都会很好。我确定我做错了什么,但我相信我已经完成了错误提示的所有事情。
谢谢。
您当前的 Startup.cs
文件已为一个数据库 (ApplicationDBContext
) 配置了数据库提供程序,但不是您正在使用的数据库 (PartsDbContext
)。
因此,添加以下行:
services.AddDbContext<PartsDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
)
我在 运行 我的 .Net Core 应用程序时遇到错误。我开始访问 DbContext 并弹出
System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.'
我已经尝试修复它,但它仍然出现。
DbContext.cs
public class PartsDbContext : DbContext
{
public DbSet<Tower> Towers { get; set; }
public DbSet<Motherboard> Motherboards { get; set; }
public PartsDbContext(DbContextOptions<PartsDbContext> options)
: base(options)
{
}
}
Controller.cs
public class AdminController : Controller
{
private readonly PartsDbContext _context;
public AdminController(PartsDbContext context)
{
_context = context;
}
public IActionResult Index()
{
if (User.Identity.Name == null)
{
return RedirectToAction("Register", "Account");
}
return View();
}
public IActionResult Towers()
{
var model = _context.Towers.ToList();
return View(model);
}
public IActionResult Motherboards()
{
var model = _context.Motherboards.ToList();
return View(model);
}
}
错误出现在 Towers() 的这一行
var model = _context.Towers.ToList();
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddEntityFramework()
.AddDbContext<PartsDbContext>();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
任何帮助都会很好。我确定我做错了什么,但我相信我已经完成了错误提示的所有事情。
谢谢。
您当前的 Startup.cs
文件已为一个数据库 (ApplicationDBContext
) 配置了数据库提供程序,但不是您正在使用的数据库 (PartsDbContext
)。
因此,添加以下行:
services.AddDbContext<PartsDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
)