【问题标题】:ASP.NET MVC unable to retrieve data from local SQL Server Express databaseASP.NET MVC 无法从本地 SQL Server Express 数据库中检索数据
【发布时间】:2016-06-20 05:16:04
【问题描述】:

首先请原谅,我是 ASP.NET 的新手,因为我目前正在尝试学习其中的一些内容。

我正在关注 ASP.NET MVC4 的在线教程之一,但我正在使用 Visual Studio 2015 Community (MVC5) 和 SQL Server 2014 Express。

目标是使用 Windows 身份验证从本地 SQL Server Express 数据库中检索数据。在这种情况下,员工的 ID、姓名、城市和性别。此时我收到以下错误:

EntityFramework.SqlServer.dll 中出现“System.Data.Entity.Core.EntityException”类型的异常,但未在用户代码中处理

附加信息:底层提供程序在打开时失败。

数据库如下所示 - ID 为 int,其余为 varchar(50)

Employee.cs 位于模型中:

[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.cs

public class EmployeeContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}

EmployeeController.cs

public class EmployeeController : Controller
{
        // GET: Employee
        public ActionResult Details(int id)
        {
            EmployeeContext employeeContext = new EmployeeContext();
            Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
            return View(employee);
        }
}

视图 - Details.cshtml

@model MVCDemo.Models.Employee

@{
    ViewBag.Title = "Employee Details";
}

<h2>Employee Details</h2>
<table style="font-family:Arial">
    <tr>
        <td>
            <b>Employee ID:</b>
        </td>
        <td>
            @Model.EmployeeId
        </td>
    </tr>
    <tr>
        <td>
            <b>Name:</b>
        </td>
        <td>
            @Model.Name
        </td>
    </tr>
    <tr>
        <td>
            <b>Gender:</b>
        </td>
        <td>
            @Model.Gender
        </td>
    </tr>
    <tr>
        <td>
            <b>City:</b>
        </td>
        <td>
            @Model.City
        </td>
    </tr>
</table>

最后,来自web.config 的连接字符串在根(我确定)目录中:

<add name="EmployeeContext" 
     connectionString="Data Source=DESKTOP-6RQ94N9\MAIN;Initial Catalog=Sample;Integrated Security=SSPI"
     providerName="System.Data.SqlClient" />

默认的connectionString="Data Source=.\SQLEXPRESS; 根本不起作用。 无论如何,服务器资源管理器显示与数据库的数据连接成功,我可以在 Visual Studio 中浏览表及其列。

当我启动项目并尝试例如:

http://localhost/MVCDemo/Employee/Details/1

网站只是保持加载一段时间,然后上面提到的异常发生在

Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);

当我调试应用程序时,employeeContext.Employees.Local.Count 显示为 0,所以我猜没有检索到数据。

非常感谢您的帮助!

【问题讨论】:

    标签: c# asp.net sql-server asp.net-mvc-5


    【解决方案1】:

    好吧,如果使用该选项,请参阅How to: Access SQL Server Using Windows Integrated Security 了解详细信息。

    您可以在 Visual Studio(或 SSMS)中以“您”的身份连接到 SQL,因为您以“您”的身份(您的 Windows 用户帐户)运行 Visual Studio。您的 Asp.Net 站点不是

    您可以使用 SQL 身份验证(代替),这样您的应用程序 (ASP.Net) 和数据存储 (SQL) 用户是分开的,因此可以单独管理,而不必处理(创建)实际 Windows 用户。如果/当您进行生产/托管时,这一点会更加明显(例如,Web 服务器和 sql 服务器是分开的,没有 Windows“域”等)。

    关于连接性问题,请参阅How to Troubleshoot Connecting to the SQL Server Database Engine

    Hth.

    【讨论】:

      【解决方案2】:

      您确定employeeContext.Employees.Local.Count 甚至会尝试调用数据库吗?您需要检查 SQL Server Express 实例的名称。在“开始”菜单的所有程序中转到带有Microsoft SQL Server (your version) 的文件夹,然后单击Sql Server Configuration Manager。在SQL Server Services 选项卡中,您会看到SQL Server (NAME),如果您在安装过程中没有更改名称,通常会显示SQL Server (SQLEXPRESS)
      此外,如果您能够使用 SQLEXPRESS 名称从 Visual Studio 访问 SQL Server,那么您可以轻松地从本地运行的 ASP.NET 网站访问它。我的一个项目具有以下连接字符串:

      <add name="DatabaseContext" connectionString="Data Source=DESKTOP-111111N\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" />
      

      请尝试像这样调用您的数据库:employeeContext.Employees.Count()
      我希望这会帮助你!

      更新

      另请检查您的数据库上下文类构造函数中设置的连接字符串,对于我的项目,它是base("DatabaseContext")

      public class DatabaseContext : DbContext
      {
          public DatabaseContext()
              : base("DatabaseContext")
          {
          }
      
      }
      


      对于您的项目,它将是base("EmployeeContext")

      【讨论】:

      • 在 Sql Server 配置管理器中检查了它,我确实将服务器名称更改为 MAIN。此时,连接字符串如下所示:&lt;add name="EmployeeContext" connectionString="Data Source=.\MAIN;Initial Catalog=Sample;Integrated Security=true" providerName="System.Data.SqlClient" /&gt;,但仍然没有结果。
      • 您使用什么名称从 Visual Studio Server Explorer 访问它?
      • 我正在使用 ./MAIN 服务器,然后从下拉列表中选择我的 Sample 数据库。一切正常。在 VS 中,我可以轻松浏览表格数据。我还使用 `base("EmployeeContext") 创建了这个空构造函数。没有变化。
      • 你是什么意思你创建了这个构造函数?你不是用DbContext Generator tool吗?当我需要创建 Model-First 应用程序时,我喜欢使用这个工具,它可以节省很多时间。
      【解决方案3】:

      好吧,在这种情况下,我实际上通过创建具有 SELECT 权限的用户并使用登录凭据更新了我的连接字符串来解决问题。

      之后,我就成功地从数据库中检索到了我需要的信息。谢谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-22
        • 2011-06-28
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 2013-12-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多