[英]error in Data access mvc using entity framework
错误消息是:
无效的列名'EmployeeId'无效的列名'EmployeeId'无效的列名'City'。
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id); //This line is causing the error
}
但是我得到了完全相同的数据库表,其中包含所有匹配的列,为什么它说我的列无效? 我哪里做错了? 我使用codeFirst方法,实际上表中有四列,分别是EmployeeID,姓名,性别,城市,为什么我没有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; }
}
}
EmployeeContext:
namespace MVCDemo.Models
{
[Table("tblEmployee")]
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
为什么使用[Table(“ tblEmployee”)]装饰EmployeeContext? 您应该改为使用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; }
}
我认为是:
EmployeeContext employeeContext = new EmployeeContext();
该行针对本地数据库服务器创建上下文。 请确保输入正确的连接字符串作为参数:
EmployeeContext employeeContext = new EmployeeContext(connection-string);
连接字符串应取自web.config或app.config,具体取决于您正在处理的项目。
您提供的详细信息不足以理解您的问题。 首先,请确保您已正确连接到数据库。 其次,如果City是您实体类中的导航属性,则可能您缺少“ Include ”方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.