【发布时间】:2017-01-15 10:18:07
【问题描述】:
错误信息是:
列名“EmployeeId”无效 列名“EmployeeId”无效 列名“城市”无效。
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id); //This line is causing the error
}
但是我得到了包含所有匹配列的完全相同的数据库表,为什么它说我有无效的列?我哪里做错了?我使用了codeFirst方法,实际上表中有四列是EmployeeID,Name,Gender,City,为什么我的Name和Gender没有错误,而EmployeeID和City只有错误? EmployeeID 中的错误出现了两次?
详细代码
员工类:
namespace MVCDemo.Models
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
}
员工上下文:
namespace MVCDemo.Models
{
[Table("tblEmployee")]
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
【问题讨论】:
-
您认为您连接到哪个数据库?您使用的是 DatabaseFirst 还是 CodeFirst 方法?
-
显示您的 EmployeeContext 和 Employee 模型代码。
-
@abdul 已将其添加到我的帖子中
标签: c# sql-server asp.net-mvc entity-framework