C# 我的ASP.NET MVC项目没有检索我的SQL Server Express值
我是ASP.NET MVC新手,目前从我在C# 我的ASP.NET MVC项目没有检索我的SQL Server Express值,c#,sql,asp.net-mvc,asp.net-mvc-4,C#,Sql,Asp.net Mvc,Asp.net Mvc 4,我是ASP.NET MVC新手,目前从我在web.config文件中指定的SQL Server数据库检索值时遇到问题,基本上,我创建了一个类库项目,其中包含Employee和Department类和IDepartmentData接口,该接口具有IQueryable和IQueryable 下面是我的课程的样子: namespace e.Manager.Data { public class Department { public int Id { set; get;
web.config
文件中指定的SQL Server数据库检索值时遇到问题,基本上,我创建了一个类库项目,其中包含Employee
和Department
类和IDepartmentData
接口,该接口具有IQueryable
和IQueryable
下面是我的课程的样子:
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; }
}
}
这是我的连接字符串:
<connectionStrings>
<add name="DepartmentDb"
connectionString="database=sample;server=CON0389\SQLEXPRESS; Integrated Security=SSPI"
providerName="System.Data.SqlClient"/>
</connectionStrings>
但是我的索引没有检索数据库中的值。我在这里做的是不是有什么错误?提前谢谢你们 谢谢你们的回答,我已经找出了项目中的错误,问题是我没有将对象映射到SQL中的表,这就是count返回0的原因 在调试代码时是否与数据库建立了连接?它会抛出错误吗?您的数据库是否注册了已建立的连接?您是否在事件查看器中看到错误?我们没有足够的信息来帮助您诊断为什么您的连接在没有至少一条错误消息的情况下无法工作。该用户在SQL server中是否具有登录名和正确的用户权限?SQL server配置管理器中是否启用了“命名管道”?获取所有的
DbObj.Departments
,然后调用变量来容纳他们员工,这是否真的很混乱,也违背了最小惊喜原则。。。。。这些员工或部门也是如此?要么变量名完全混淆,或者,DbObj.Departments
返回的不是它的名称所暗示的……嗨,乔治,你说得对,实际上它应该是这样的Departments=DbObj.Departments,它似乎连接到数据库,但没有检索到任何值谢谢你们的回答我已经找出了我项目中的错误,问题是我没有将我的对象映射到SQL中的表,这就是count返回0的原因。
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; }
}
}
}
<connectionStrings>
<add name="DepartmentDb"
connectionString="database=sample;server=CON0389\SQLEXPRESS; Integrated Security=SSPI"
providerName="System.Data.SqlClient"/>
</connectionStrings>
public class HomeController : Controller
{
private DepartmentDb DbObj = new DepartmentDb();
public ActionResult Index()
{
var department= DbObj.Departments;
return View(department);
}
}