【问题标题】:Not Able to Access Attribute of a Constructor in a method [closed]无法在方法中访问构造函数的属性[关闭]
【发布时间】:2020-12-16 06:15:07
【问题描述】:

我正在学习 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 在当前上下文中不存在

我尝试了很多解决方案,但没有出现错误。我也不明白为什么会出现错误。帮帮我。

【问题讨论】:

    标签: c# entity-framework model-view-controller .net-core


    【解决方案1】:

    你应该使用_dbContext,而不是dbContext

    【讨论】:

    猜你喜欢
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多