我在mvc4中得到以下错误,';System.Data.Entity.Core.EntityCommandExec

本文关键字:System Data Entity EntityCommandExec Core mvc4 错误 我在 | 更新日期: 2023-09-27 18:27:28

我在行中得到以下错误

Employee employee = employeeContext.Employees.Single(x => x.EmployeeId==id)

类型的异常中出现"System.Data.Entity.Core.EntityCommandExecutionException"EntityFramework.SqlServer.dll,但未在用户代码中处理

我的控制器是

public class EmployeeController : Controller
{
    //
    // GET: /Employee/
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Details(int id)
    {
        EmployeeContext employeeContext = new EmployeeContext();
        Employee employee = employeeContext.Employees.Single(x =>x.EmployeeId==id); 
        return View(employee);
    }
}

型号类别为:

[Table("tblEmployee")]
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
}

上下文类为:

我在mvc4中得到以下错误,';System.Data.Entity.Core.EntityCommandExec

通过更改来尝试

Employee employee = employeeContext.Employees.Single(x =>x.EmployeeId==id); 

进入

Employee employee = employeeContext.Employees.Where(x =>x.EmployeeId==id).FirstOrDefault;