简体   繁体   English

我的ASP.NET MVC项目未获取我的SQL Server Express值

[英]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数据库中检索值时遇到问题,基本上,我已经创建了一个具有EmployeeDepartment类以及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 我的表包含相关的表PersonDepartment

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.

相关问题 如何在ASP.NET Core项目中指向我的完整SQL Server Express数据库? - How do I point to my full SQL Server Express database in ASP.NET Core project? 如何将SQL Server CE用于我的ASP.net MVC成员/授权项目? - How can I use SQL Server CE for my ASP.net MVC membership/authorization project? ASP.NET MVC 4-如何在删除旧项目的同时将SQL Server Express DB移至新项目? - ASP.NET MVC 4 - How to move SQL Server Express DB to new project while deleting old project? ASP.NET MVC:从 SQL 服务器快速移动到 Azure - ASP.NET MVC: moving from SQL Server Express to Azure 我希望我的ASP.NET MVC项目成为服务器上的子站点。 我该怎么做呢? - I want my ASP.NET MVC project to be a subsite on my server. How do I do this? 如何在默认的asp.net mvc项目中将值插入SQL Server中的现有行? - How do I insert values into existing rows in SQL server in default asp.net mvc project? 如何在 asp.net mvc 项目中管理我的 SQL 查询脚本? - How can I manage my SQL query script in asp.net mvc project? 有没有办法在 ASP.NET MVC 5 中包含我的项目之外的图像? - Is there a way to include images outside of my project in ASP.NET MVC 5? WebMatrix可以编译My Asp.net MVC项目吗? - Can WebMatrix Compile My Asp.net MVC Project ? 编辑在我的 ASP.NET MVC 5 项目中不起作用或无法正常工作 - Edit is not working or functioning in my ASP.NET MVC 5 project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM