无法访问方法中构造函数的属性
Not Able to Access Attribute of a Constructor in a method
我正在学习 DotNet Core。
这是我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Practinig_Student.Models;
using Microsoft.EntityFrameworkCore;
using System.Data.Entity;
namespace Practinig_Student.Controllers
{
public class EmployeeController : Controller
{
private readonly EmployeeContext _dbcontext;
public EmployeeController(EmployeeContext dbContext)
{
_dbcontext = dbContext;
}
public IActionResult Employee_Details(int id)
{
// EmployeeContext employeeContext = new EmployeeContext();
//Employee employee = dbContext.Employees.Single(emp => emp.Id == id);
var emplst = dbContext.Employees.Single(emp => emp.Id == id);
return View(emplst);
}
}
}
我在一行中遇到错误
var emplst = dbContext.Employees.Single(emp => emp.Id == id);
我无法访问的地方 dbContext
我得到的错误是 名称 dbContext 在当前上下文中不存在
我尝试了很多解决方案,但错误并没有消失。我也不明白为什么会出现错误。帮帮我。
你应该使用 _dbContext
,而不是 dbContext
我正在学习 DotNet Core。 这是我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Practinig_Student.Models;
using Microsoft.EntityFrameworkCore;
using System.Data.Entity;
namespace Practinig_Student.Controllers
{
public class EmployeeController : Controller
{
private readonly EmployeeContext _dbcontext;
public EmployeeController(EmployeeContext dbContext)
{
_dbcontext = dbContext;
}
public IActionResult Employee_Details(int id)
{
// EmployeeContext employeeContext = new EmployeeContext();
//Employee employee = dbContext.Employees.Single(emp => emp.Id == id);
var emplst = dbContext.Employees.Single(emp => emp.Id == id);
return View(emplst);
}
}
}
我在一行中遇到错误
var emplst = dbContext.Employees.Single(emp => emp.Id == id);
我无法访问的地方 dbContext
我得到的错误是 名称 dbContext 在当前上下文中不存在
我尝试了很多解决方案,但错误并没有消失。我也不明白为什么会出现错误。帮帮我。
你应该使用 _dbContext
,而不是 dbContext