[英]My ASP.NET MVC project is not retrieving my SQL Server Express values
I am new to ASP.NET MVC and I am currently having problem retrieving values from my SQL Server database that I have specified in my web.config
file, Basically I have created a class library project which has the Employee
and Department
classes and IDepartmentData
interface that has the IQueryable<Employee>
and IQueryable<Department>
我是ASP.NET MVC的新手,目前在从我在
web.config
文件中指定的SQL Server数据库中检索值时遇到问题,基本上,我已经创建了一个具有Employee
和Department
类以及IDepartmentData
接口的类库项目具有IQueryable<Employee>
和IQueryable<Department>
Here is how my classes look like: 这是我的班级样子:
namespace e.Manager.Data
{
public class Department
{
public int Id { set; get; }
public string Name { get; set; }
public ICollection<Employee> Employees { get; set; }
}
}
namespace e.Manager.Data
{
[Table("Person")]
public class Employee
{
public virtual int Id { set; get; }
public virtual string Name { set; get; }
}
}
and here is my iInterface: 这是我的iInterface:
namespace e.Manager.Data
{
public interface IDepartmentData
{
IQueryable<Employee> Employees { get; }
IQueryable<Department> Departments { get; }
}
}
and of course I have a reference of this project in my ASP.NET MVC web project. 当然,我在ASP.NET MVC Web项目中也有此项目的参考。
Here's my class DepartmentDb
: 这是我班的
DepartmentDb
:
namespace eManager.Web.Infrastructure
{
public class DepartmentDb : DbContext, IDepartmentData
{
public DbSet<Employee> Employees { set; get; }
public DbSet<Department> Departments { set; get; }
IQueryable<Employee> IDepartmentData.Employees
{
get { return Employees; }
}
IQueryable<Department> IDepartmentData.Departments
{
get { return Departments; }
}
}
}
and here is my connection string: 这是我的连接字符串:
<connectionStrings>
<add name="DepartmentDb"
connectionString="database=sample;server=CON0389\SQLEXPRESS; Integrated Security=SSPI"
providerName="System.Data.SqlClient"/>
</connectionStrings>
and my table contains the relevant tables Person
and Department
我的表包含相关的表
Person
和Department
Here is my Index action method: 这是我的索引操作方法:
public class HomeController : Controller
{
private DepartmentDb DbObj = new DepartmentDb();
public ActionResult Index()
{
var department= DbObj.Departments;
return View(department);
}
}
but my index doesn't retrieve the values that are within the DB is there is something wrong that I am doing here? 但是我的索引没有检索数据库中的值,这是我在做什么错吗? Thank you guys in advance.
预先谢谢你们。
谢谢您的回答,我已经弄清楚了我的项目出了什么问题,问题是我没有将对象映射到SQL中的表,这就是为什么count返回0的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.