【问题标题】:An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user codeEntityFramework.SqlServer.dll 中出现“System.Data.Entity.Core.EntityException”类型的异常,但未在用户代码中处理
【发布时间】:2016-11-06 00:59:16
【问题描述】:

我是 ASP.NET MVC 的新手,我遇到了这个异常,连接字符串看起来很完美,但仍然引发了异常,如果有人告诉我为什么会发生,不胜感激。

谢谢大家

模型 1

namespace MVCTwice.Models
{
    public class StudentContext : DbContext
    {
        public DbSet<Student> studs { get; set; }
    }
}

模型 2

namespace MVCTwice.Models
{
    [Table("tblStudents")]
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }
        public string gender { get; set; }
        public string totalMarks { get; set; }
    }
}

动作方法

public ActionResult Index()
        {
            StudentContext studentContext = new StudentContext();
            //Student emp = studentContext.studs.Select(emp=>emp.)
           List<Student> emp=studentContext.studs.ToList();

            return View(emp);
        }

查看

@model MVCTwice.Models.Student
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        @Model.gender
        @Model.name
        @Model.id
        @Model.totalMarks

    </div>
</body>
</html>

例外

连接字符串

 <connectionStrings >
    <add
         name="myConnectionString"
         connectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=LoginInfo;Data Source=.\SQLEXPRESS"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>

【问题讨论】:

  • 您可以使用来自 sql mgmnt studio 的凭据登录吗?我会从那里开始。
  • 可以,登录 sql mgmnt studio
  • 您没有在连接字符串中提供密码
  • 提供密码后异常保持不变
  • 显示您的内部异常和堆栈跟踪以获取更多详细信息。通常内部异常会说明您应该如何解决连接问题。

标签: asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-4 entity-framework-6


【解决方案1】:
<connectionStrings>
  <add name ="StudentContext " 
        connectionString ="server=.; database=here your database name; integrated security=SSPI"
           providerName ="system.data.SqlClient"/>
</connectionStrings>

这是您的代码,但更改您的数据库名称,然后将其添加到 web.config 文件中。

【讨论】:

    【解决方案2】:

    转到 iis -> 应用程序池 -> 找到您在应用程序中使用的应用程序池 -> 单击它,然后单击“操作”面板中的“高级设置”。找到 'Identity' 属性并将其更改为 localsystem。

    如果项目运行成功,请运行该项目,如果没有,则很好,您的解决方案可能在下面的链接中。

    修复上述步骤后,有些人会遇到另一个错误,提示“SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'”

    因此,请点击以下链接: SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'

    https://dba.stackexchange.com/questions/47172/can-not-find-nt-authority-network-service-on-sql-server-2012

    【讨论】:

      【解决方案3】:

      这里是这个异常的答案 ---> 更改连接字符串的名称,名称必须与您的上下文类相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-31
        • 2016-02-13
        • 2018-10-25
        • 2016-09-21
        • 2015-05-30
        • 1970-01-01
        相关资源
        最近更新 更多