刚开始学MVC,希望大大们多多指教。问题是这样的:我在查询数据库的时候,我的表为student.运行程序的时候总是报"dbo.students对象无效",调试发现,怎么在表后面加了一个"s"呢,我的数据库中的表就是student,并没有加"s"啊。我附上代码:
M层:
public class Student
{
[Key]
public string Sno { get; set; }
public string Sname { get; set; }
public string Sid { get; set; }
public string Ssex { get; set; }
public DateTime Sbrithday { get; set; }
public string Sdept { get; set; }
}
public class EmplloyeeDbContext : DbContext
{
public DbSet<Student> student { get; set; }
}
C层:
EmplloyeeDbContext db = new EmplloyeeDbContext();
public ActionResult Index()
{
var employee = from e in db.student
where e.Sbrithday<DateTime.Now
select e;
return View(employee.ToList());
}
生成的SQL语句:
SELECT
[Extent1].[Sno] AS [Sno],
[Extent1].[Sname] AS [Sname],
[Extent1].[Sid] AS [Sid],
[Extent1].[Ssex] AS [Ssex],
[Extent1].[Sbrithday] AS [Sbrithday],
[Extent1].[Sdept] AS [Sdept]
FROM [dbo].[Students] AS [Extent1]
WHERE [Extent1].[Sbrithday] < (SysDateTime())
这里的 表就变成了students. 等待解答,,谢谢。。
你是不是用了Entity Framework,Entity Framework在创建数据库时会在实体后加一个S,可以通过Table("Student")来自定义为Student:
http://www.cnblogs.com/yangyancheng/archive/2011/05/12/2042922.html(见Table)
感谢您。